我在从具有可更新内容的视图中绑定复杂模型时遇到问题,首先,实体:
public partial class Diagnosis
{
public Diagnosis()
{
this.DiagZones = new HashSet<DiagZone>();
...
}
public int diagnosisid { get; set; }
public int playerid { get; set; }
public int userid { get; set; }
...
public virtual ICollection<DiagZone> DiagZones { get; set; }
}
我的DiagZones
集合是中间表Diagnosis
,Zones
但它存在于我的模型中,因为它的字段比 id 的多。
我有一个 Select 控件,您可以在其中选择和取消选择Zones
,当onchange
触发时,I Get a Partial View with a ajax call。
编码:
编辑诊断.cshtml
@model Gfire.Models.DiagnosisViewModel
<h2>
@ViewBag.playername</h2>
@using (Html.BeginForm("EditDiagnosis", "Diagnosis", FormMethod.Post))
{
@Html.HiddenFor(d => d.Diagnosis.diagnosisid)
<table>
...
</table>
<table>
<tr>
<td>
Zones:
</td>
<td>
@Html.ListBoxFor(d => d.SelectedZones, new SelectList(Model.Zones, "zoneid", "description"), new
{
style = "width:305px;",
onchange = "QualityMovement(this);",
id = "lbZone",
@class = "chzn-select"
})
</td>
<td>
...
</td>
</tr>
</table>
<div id="qualitymovement">
@Html.Partial("_QualityMovement", Model.Diagnosis.DiagZones)
</div>
<div>
<input type="submit" value="Save" />
  |   @Html.ActionLink("Cancel", "IndexDiagnosisPlayer", new { playerid = ViewBag.playerid })
</div>
}
局部视图 (_QualityMovement.cshtml):
@model List<Gfire.Domain.Entities.Diagnosis.DiagZone>
@if (Model.Count() != 0)
{
<table>
@foreach (var dz in Model)
{
<tr>
<tr>
<td>
Flex:
</td>
<td>
@Html.TextBoxFor(d => dz.flex))
</td>
</tr>
</tr>
}
</table>
}
Ajax.Get
来电:
<script type="text/javascript" language="javascript">
function JointBalance(item) {
...
$.ajax({
url: '@Url.Action("GetJointBalances", "Diagnosis")',
data: arrayToParamObject('zonesid', items),
contentType: "application/json; charset=utf-8",
success: function (data) {
// Successful requests get here
$("#jointbalance").html(data);
$("#jointbalance").fadeIn('slow');
},
type: "GET",
datatype: "json"
});
...
}
</script>
在服务器中,我有一个方法可以初始化新列表DiagZones
并正确更新EditView.cshtml
.
当我尝试提交Diagnosis
包含所有字段和 DiagZones 列表的完整对象时出现问题,但我的方法是:
[HttpPost]
public ActionResult EditDiagnosis(DiagnosisViewModel DiagnosisViewModel)
{
if (ModelState.IsValid)
{
// Save the model
...
return RedirectToAction("IndexDiagnosisPlayer", new { playerid = SessionHelper.Player.playerid });
}
else
{
return View("EditDiagnosis", new { diagnosisid = DiagnosisViewModel.diagnosisid });
}
}
我的模型已清空DiagnosisViewModel.DiagZones
列表。
我尝试使用 EditorFor,将完整模型传递给部分视图,添加几个表单......但它没用,我怎样才能将该列表绑定到我的模型?
提前致谢。
更新
这是服务器端操作所期望的:
[HttpGet]
public ActionResult GetJointBalances(int[] zonesid) { ... }
GET ajax 请求数据如下所示:
Request URL:http://localhost/Gfire.WebUI/Diagnosis/GetQualityMovement?zonesid=47
Request Method:GET
Status Code:200 OK
Request Headersview source
...
Connection:keep-alive
Content-Type:application/json; charset=utf-8
...
Referer:http://localhost/Gfire.WebUI/Diagnosis/EditDiagnosis?diagnosisid=0&playerid=23
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.152 Safari/537.22
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
zonesid:47