如何在asp.net mvc中回发期间持久保存部分视图模型数据 [英] how to persist partial view model data during postback in asp.net mvc
问题描述
我正在部分查看包含部门信息的部门.我有雇员的页面视图.在员工视图中,我正在使用部门的局部视图来显示员工部门.我的员工模型如下
class员工{公共字符串EmployeeName {get; set};公共部门EmployeeName {get; set};}课系{公共字符串DepartmentName {get; set};}
我在员工页面视图上有提交"按钮.当我提交员工视图时,我将Department对象设置为null.您能否建议我在回发期间如何获得儿童部门的模型.管理员代码
[HttpGet]public ActionResult Employee2(){雇员e =新雇员();e.EmployeeName ="Prashant";e.Department = new Department(){DepartmentName ="Phy"};返回View(e);}[HttpPost]public ActionResult Employee2(雇员e){返回View(e);}
观看次数
部门
<代码> @model MvcApplication2.Models.Department< script src =〜/Scripts/jquery-1.7.1.min.js"></script>< script src =〜/Scripts/jquery.validate.min.js"></script>< script src =〜/Scripts/jquery.validate.unobtrusive.min.js"></script>< fieldset>< legend>部门</legend>< div class ="editor-label">@ Html.LabelFor(模型=>模型.部门名称)</div>< div class ="editor-field">@ Html.EditorFor(模型=>模型.部门名称)@ Html.ValidationMessageFor(model => model.DepartmentName)</div></fieldset>< div>@ Html.ActionLink(返回列表",索引")</div>
员工
<代码> @model MvcApplication2.Models.Employee@ {ViewBag.Title =员工";}< h2>雇员</h2>@using(Html.BeginForm("Index","Home")){< fieldset>< legend>员工</legend>< div class ="editor-label">@ Html.LabelFor(model => model.EmployeeName)</div>< div class ="editor-field">@ Html.EditorFor(model => model.EmployeeName)@ Html.ValidationMessageFor(model => model.EmployeeName)</div>@ Html.Partial(部门",模型.部门)< p><输入type ="submit" value ="EmployeeSave"/></p></fieldset>}@section脚本{@ Scripts.Render(〜/bundles/jqueryval")}
尝试先搜索.这个问题已经被问过很多次了.
这是一种实现方法:
摘要:将每个部分包装在多个表单标签中,每个标签都有自己的提交按钮.
但是这个似乎更像是你所追求的:
为此使用编辑器模板,而不是局部的.
您遇到的问题是,当您的DepartmentName文本框未正确命名时,控制器无法读取它.您的 POST
将为 EmployeeName = Prashant& DepartmentName = Phy
,因此 Department
为 null
,因此会出现错误.>
I am having partial view Department containing department information. I have Page view of Employee. in Employee view i am using partial view of department to show employees department. My employee model is as below
class Employee
{
public string EmployeeName{get;set};
public Department EmployeeName{get;set};
}
class Department
{
public string DepartmentName{get;set};
}
I have submit button on employee page view. When i submit the employee view I am getting Department object as null. Could you please suggest me how i can get child Department model during postback. Coltroller code
[HttpGet]
public ActionResult Employee2()
{
Employee e = new Employee();
e.EmployeeName = "Prashant";
e.Department = new Department() { DepartmentName = "Phy" };
return View(e);
}
[HttpPost]
public ActionResult Employee2(Employee e)
{
return View(e);
}
Views
Department
@model MvcApplication2.Models.Department
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<fieldset>
<legend>Department</legend>
<div class="editor-label">
@Html.LabelFor(model => model.DepartmentName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DepartmentName)
@Html.ValidationMessageFor(model => model.DepartmentName)
</div>
</fieldset>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Employee
@model MvcApplication2.Models.Employee
@{
ViewBag.Title = "Employee";
}
<h2>
Employee</h2>
@using (Html.BeginForm("Index","Home"))
{
<fieldset>
<legend>Employee</legend>
<div class="editor-label">
@Html.LabelFor(model => model.EmployeeName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmployeeName)
@Html.ValidationMessageFor(model => model.EmployeeName)
</div>
@Html.Partial("Department", Model.Department)
<p>
<input type="submit" value="EmployeeSave" />
</p>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Try searching first. This question has been asked many times before.
Here is one way to do it:
Summary: wrap each partial in multiple form tags each with their own submit button.
But this one seems more like what you're after:
Post a form with multiple partial views
Use editortemplates for this instead of partials.
The issue you're having is that when your DepartmentName textbox isn't being named correctly for your controller to read it. Your POST
would be EmployeeName=Prashant&DepartmentName=Phy
therefore Department
is null
, hence the error.
这篇关于如何在asp.net mvc中回发期间持久保存部分视图模型数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!