试试这个修改你的局部视图
//In Partial View
@model MvcApplication1.Employee
<table>
<tr>
<td>
@Html.TextBoxFor(m => m.Id)
</td>
<td>
@Html.TextBoxFor(m => m.Name)
</td>
<td>
@Html.TextBoxFor(m => m.Code)
</td>
</tr>
</table>
Now in your parent view
// In View.cshtml
<div id="div_employee">
@Partail("partailView",Model)
</div>
@using (Ajax.BeginForm("Employee", "PO", new AjaxOptions { HttpMethod = "GET", UpdateTargetId = "div_employee", InsertionMode = InsertionMode.Replace }))
{
<input type="submit" value="save" />
}
在控制器中
public string Employee(Employee em)
{
var em1 = new Employee
{
Id = 1,
Name = "xing yanguang",
Code = "131324e12"
};
return RenderPartialViewToString("partailView",em1);
}
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
在控制器中添加另一种方法来获取部分视图的 RenderHtml,通过将ParttailViewName和模型传递给 RenderPartialViewToString 方法,它将返回具有更新值的视图 Htmlstring。