在我的示例 MVC 应用程序中,我有一个模型
class SampleModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<Certification> Certifications { get; set; }
}
class Certification
{
public int Id { get; set; }
public string CertificationName { get; set; }
public int DurationInMonths { get; set; }
}
我的视图(我需要在部分视图中显示认证详细信息)
@model SampleApplication.Model.SampleModel
<!-- other code... -->
@using (Html.BeginForm("SaveValues","Sample", FormMethod.Post, new { id= "saveForm" }))
{
@Html.HiddenFor(m => m.Id, new { id = "hdnID" })
@Html.TextBoxFor(m => m.Name, new { id = "txtName" })
@{Html.RenderPartial("_CertDetails.cshtml", Model.Certifications);}
<input type="submit" id="btnSubmit" name="btnSubmit" value="Update" />
}
局部视图
@model List<SampleApplication.Model.Certification>
<!-- other code... -->
@if (@Model != null)
{
for (int i = 0; i < @Model.Count; i++)
{
@Html.HiddenFor(m => m[i].Id , new { id = "CId" + i.ToString() })
@Html.TextBoxFor(m => m[i].CertificationName,new{ id ="CName" + i.ToString() })
@Html.TextBoxFor(m => m[i].DurationInMonths,new{ id ="CDur" + i.ToString() })
}
}
控制器
[HttpPost]
public ActionResult SaveValues(SampleModel sm)
{
//Here i am not getting the updated Certification details (in sm)
}
表单发布后如何在控制器中获取部分视图的更新值?当我不使用 partialview 时,我能够获得更新的认证值。这是正确的方法还是我应该遵循其他方法?