我已经为此工作了几个小时。我有一个如下所示的局部 View
_Partial.cshtml
@model project.Jobs
@using (Html.BeginCollectionItem("Jobs"))
{
<tr id="jobrow">
<td>
@Html.HiddenFor(modelItem => modelItem.JobId)
@Html.EditorFor(modelItem => modelItem.Description, new { htmlAttributes = new { @class = "" } })
</td>
<td>
@Html.EditorFor(modelItem => modelItem.FlatRate, new { htmlAttributes = new { @class = "", type = "number", min = "0.00" } })
</td>
<td>
@Html.EditorFor(modelItem => modelItem.StandardHour, new { htmlAttributes = new { @class = "", type = "number", min = "0.00" } })
</td>
<td>
@Ajax.ActionLink("Click Me", "AddRepairOrderJobs", "RepairOrderWizard", Model, new AjaxOptions { InsertionMode = InsertionMode.InsertAfter, UpdateTargetId = "rodetailrowdata", HttpMethod = "POST" })
</td>
</tr>
}
这是我渲染局部 View 的地方
主视图.cshtml
@model project.MainViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table id="mygrid" class="table">
<thead>
<tr>
<th>Description</th>
<th>Flat Rate</th>
<th>Standart Time</th>
<th>Charge To</th>
<th></th>
</tr>
</thead>
<tbody id="rodetailrowdata">
@foreach (var item in Model.MainViewModel.Jobs)
{
@Html.Partial("_Partial", item)
}
</tbody>
</table>
使用下面的操作方法,我可以使用上面的 ajax.actionlink 将来自局部 View 的 editorfor 标签的现有数据传递到 Controller
Controller.cs
[HttpPost]
public PartialViewResult AddRepairOrderJobs(Jobs job)
{
var x = new OtherModel(){
JobId = job.JobId,
Name = job.Description,
SelectedRate = job.FlatRate
StandardHour = job.StandardHour
}
return PartialView("_HeaderDetailList", x);
}
现在,我想做的是,当我修改 editorfor 的值时,我从 _Partial.cshtml 获取更新的数据,而不是单击操作链接时的旧数据,但我不能'让这个工作。 我如何在不尽可能多地做一些“黑客工作”的情况下做到这一点?
编辑:我添加了加载作业列表的操作方法
public ActionResult NewRecord()
{
var add= new MainViewModel()
{
//other data,
Jobs = jobrepository.GetAllJobs()
};
return View(add);
}
最佳答案
我使用@Tomato32 的提示解决了我的问题。我只需要将 Jobs 模型放在主视图而不是局部 View 中并循环遍历它,然后将我的 View 模型作为参数而不是作业模型传递。现在我得到了一切,包括更新的工作数据。如果有人需要,我可以包含代码。
关于c# - 从部分 View 编辑器获取更新数据并传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831077/