c# - 如何将数据从局部视图传递到视图 - MVC
问题描述
请阅读 :) 提前感谢 :)
我有一个名为 RMA 的视图(http://localhost/User/RMA?Id=132),它显示了有关产品的一些详细信息,我还显示了与相关的评论(列表)该产品还有在同一个视图中更新每个评论的可能性,但首先我应该通过 id 获取评论,然后我可以在同一个视图中更新每个评论,但我确实有一些问题要在同一视图中通过 id 获取评论查看,我还显示评论列表。让我描述问题:)
- 当我尝试通过 id
http://localhost/User/CommentsId?ComID=155&id=132
或
http://localhost/User/CommentsId?ComID=145&id=132 显示评论时“不在
同一个视图中 - 一切正常"
控制器:
public ActionResult CommentsId(int? ComID, int? id)
{
//Show Comment per ID
var querythree = (from RH in db.RMA_History
join RB in db.Besked on RH.Id equals RB.RMAID
where RB.ID == ComID && RB.RMAID == id
select new VMRMA
{
RBID = RB.ID,
RBPM = RB.MSG,
RBRMAID = RB.RMAID
});
var q = querythree.FirstOrDefault();
return View(q);
}
查看(CommentsId.cshtml):
@using ModelNameSpace.Models
@model VMRMA
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>CommentsById</title>
</head>
<body>
<div>
<input type="hidden" name="ComID" value="@Model.RBID" />
<input type="hidden" name="RMAID" value="@Model.RBRMAID" />
<p>@Model.RBPM</p>
</div>
</body>
</html>
视图是 RMA ,用户点击更新链接:
@using NameSpace.Models
@model VMRMA
//Loop Comments
@foreach (var item in Model.Comment_Lists)
{
<h3 class="h5">@item.PM</h3>
//Link
<a href="/User/CommentsId?ComID=@item.ID&id=@item.RMAID">Update</a>
}
- 但是,当我尝试使用 PartialView 在同一视图 RMA
http://localhost/User/RMA?Id=132
中按 id 显示评论时,当我检查它是否有效时,我可以看到弹出窗口显示评论它是空的和它不包含任何数据!
控制器:
public ActionResult _CommentsById(int? ComID , int? id)
{
var querythree = (from RH in db.RMA_History
join RB in db.Besked on RH.Id equals RB.RMAID
where RB.ID == ComID && RB.RMAID == id
select new VMRMA
{
RBID =RB.ID,
RBPM =RB.MSG,
RBRMAID=RB.RMAID
});
var q = querythree.FirstOrDefault();
return PartialView("_CommentsById", q);
}
部分视图(_CommentsById.cshtml):
@using ModelNameSpace.Models
@model VMRMA
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>CommentsById</title>
</head>
<body>
<div>
<input type="hidden" name="ComID" value="@Model.RBID" />
<input type="hidden" name="RMAID" value="@Model.RBRMAID" />
<p>@Model.RBPM</p>
</div>
</body>
</html>
View RMA ,其中渲染 PartialView:
@using NameSpace.Models
@model VMRMA
//Select From Parent Model HRMAS
<input type="text" class="form-control disabled" value="@Model.HRMAs.FirmaNavn">
//Loop Comments list
@foreach (var item in Model.Comment_Lists)
{
<h3 class="h5">@item.PM</h3>
//Link
<a data-toggle="modal" data-target="#myModal" href="/User/_CommentsById?ComID=@item.ID&id=@item.RMAID">Update</a>
}
<div class="container">
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content--
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4></div>
<div class="modal-body">
@Html.Partial("_CommentsById")
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
RMA 控制器:
public ActionResult RMA(int? id, VMRMA model)
{
//List comments
var queryTwo = (from RH in db.RMA_History
join RB in db.Besked on RH.Id equals RB.RMAID
where RB.RMAID == id
select new VMRMA.Comment_List
{
//Select Something
});
var query = (from RH in db.RMA_History
join RS in db.RMAStatus on RH.StatusID equals RS.ID
where RH.Id == id
select new VMRMA.HRMA
{
//Select Something
});
model.HRMAs = query.FirstOrDefault();
model.Comment_Lists = queryTwo.ToList();
return View(model);
}
视图模型 VMRMA:
public class VMRMA
{
public int? RBID { get; set; }
public string RBPM { get; set; }
public int? RBRMAID { get; set; }
public Comment_PM Comment_PMs { get; set; }
public class Comment_PM
{
public Comment_PM()
{
}
public Comment_PM(string PM, int? ID, DateTime Date, string Forfatter, string whos, int? RMAID)
{
this.PM = PM;
this.ID = ID;
this.Date = Date;
this.Forfatter = Forfatter;
this.whos = whos;
this.RMAID = RMAID;
}
public int? ID { get; set; }
public string PM { get; set; }
public DateTime Date { get; set; }
public string Forfatter { get; set; }
public string whos { get; set; }
public int? RMAID { get; set; }
}
------------------------------
public HRMA HRMAs { get; set; }
public class HRMA
{
public HRMA(string FirmaNavn)
{
this.FirmaNavn = FirmaNavn;
}
public string FirmaNavn{ get; set; }
}
------------------------------
public List<Comment_List> Comment_Lists { get; set; }
public class Comment_List
{
public Comment_List()
{
}
public Comment_List(string PM, int? ID, DateTime Date, string Forfatter , string whos , int? RMAID)
{
this.PM = PM;
this.ID = ID;
this.Date = Date;
this.Forfatter = Forfatter;
this.whos = whos;
this.RMAID = RMAID;
}
public int? ID { get; set; }
public string PM { get; set; }
public DateTime Date { get; set; }
public string Forfatter { get; set; }
public string whos { get; set; }
public int? RMAID { get; set; }
}
}
- 谁能帮助我或指出正确的方向!为什么当我尝试使用部分视图在同一视图中按 id 显示评论时,它无法检索数据并且我认为它的 bcuz 无法将 ComID 值传递给控制器,当我尝试在 RMA 视图中使用时,那么我怎样才能获得 ComID 值而不是将其传递给 RMA 控制器?!
- 但是当我使用不同的视图按 id 显示评论时,它会检索数据
- 或者我如何使用 Ajax 获取 PartialView 的内容,而不是在 RMA 视图中替换它
- 或任何建议我如何在同一视图中按 id 显示评论而不是更新评论。
在此先感谢您的帮助 :)
解决方案
推荐阅读
- haskell - Haskell 中的“让”和“进入”是什么意思?
- go - 在 Golang 中定期爬取 API
- azure - 如何访问在 Azure 上创建的公共 IP 地址的 DNS 名称上的子域?
- c# - System.InvalidCastException:'无法将'System.DBNull'类型的对象转换为'System.Byte []'类型。'
- c++ - 这是我被卡住的问题。在 22 个测试用例中,有 5 个性能测试用例失败了。如何优化我的代码?
- php - 如何从树枝向控制器发送和接收请求参数
- c# - 获取乘以 2 的数字中的 int 值列表
- openshift - 在 Fluentd 中将多行日志解析为单个事件
- python-3.x - 不尝试使用任何属性时出现 AttributeError
- ssl - Websphere MQ:如何将 SSL 与托管客户端一起使用