嘗試此 修改局部視圖
//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,通過使PartailViewName和模型到RenderPartialViewToString方法將與更新後的值返回的視圖Htmlstring。
可能的重複[爲什麼我在我的模型中更新MVC3控制器中的值不在客戶端上呈現?](http://stackoverflow.com/questions/5675213/why-is-a-value-that -i-update-in-my-model-inside-an-mvc3-controller-not-rendered) – tpeczek
請檢查我提供的重複鏈接 - 這是ModelState的問題(部分視圖忽略了你的新模型,來自ModelState的值) – tpeczek
是的,確實重複了。它的作品謝謝。 –