从局部视图提交数据到控制器MVC [英] Submit Data from partial view to a controller MVC
问题描述
我有就业记录列表,还可以在同一页面使用局部视图中添加的就业记录。
I have a list of employment records, you can also add an employment record from the same page using a partial view.
这是有记录列表和局部视图中添加它出现在一个模式弹出一个新的记录的局部视图继承人employment.cshtml。
Heres employment.cshtml that has a partial view for the records list and a partial view to add a new record which appears in a modal pop up.
<h2>Employment Records</h2>
@{Html.RenderPartial("_employmentlist", Model);}
<p>
<a href="#regModal" class="btn btn_b" rel="fancyReg">Add New Record</a>
</p>
<div style="display:none">
<div id="regModal">
@{Html.RenderPartial("_AddEmployment", new ViewModelEmploymentRecord());}
</div>
</div>
继承人的局部视图_AddEmployment.cshtml
Heres the partial view _AddEmployment.cshtml
@using (Html.BeginForm("AddEmployment, Application"))
{
@Html.ValidationSummary(true)
<div class="formEl_a">
<fieldset>
<legend></legend>
<div class="sepH_b">
<div class="editor-label">
@Html.LabelFor(model => model.employerName)
</div>
etc....etc....
</fieldset>
</div>
<p>
<input type="submit" class="btn btn_d" value="Add New Record" />
</p>
}
和我的继承人应用控制器:
and heres my Application controller:
[HttpPost]
public ActionResult AddEmployment(ViewModelEmploymentRecord model)
{
try
{
if (ModelState.IsValid)
{
Add Data.....
}
}
catch
{
}
return View(model);
}
在编译以下HTML表单生成的:
When compiling the following html is generated for the form:
<form action="/Application/Employment?Length=26" method="post">
它带来了一个长度的字符串?并调用就业控制器呢?
It brings in a length string? and is invoking the Employment controller instead?
希望一切都清楚......
Hope all is clear....
问题一:当我从局部视图不转到指定添加数据控制器内点击提交按钮。任何人都可以看到我去错了?
QUESTION ONE: when I click the submit button from within the partial view it does not go to the controller specified to add the data. Can anyone see where im going wrong?
问题二::当我得到这个工作,我想用新记录更新就业名单....我要对这个正确的方式?任何提示AP preciated。
QUESTION TWO: When I get this working I would like to update the employment list with the new record....am I going about this the correct way? Any tips appreciated.
推荐答案
答1:首先试试这个,让我知道这是否击中了你的控制器
Answer 1: First try this and let me know if that hits your controller.
@using (Html.BeginForm("AddEmployment", "Application", FormMethod.Post))
答2:要更新就业名单,我会假设你想保存模型到数据库,然后有显示在同一页面或不同的页面调用从数据库中的数据到列表或对你的就业名单要显示的表格
Answer 2: To update the employment list, I would assume you would want to save the model to your database then have your employment list displayed on the same page or a different page calling the data from the DB into the the list or table to be displayed.
编辑:
它看起来好像没有被应用于表单属性。
为了您的employment.cshtml,我个人不使用{}在我@Html语句。
你不能做什么,我上文所述,因为只有当我写它作为你出现错误
It looks as though your form attributes are not being applied. For your employment.cshtml, I personally don't use { } around my @Html statements. You must not be doing what I stated above because your error occurs only when I write it as
@using (Html.BeginForm("AddEmployment, Application", FormMethod.Post))
缺少那些关闭的报价是什么原因造成您的问题。
missing those closing quotes is what is causing your problem.
这篇关于从局部视图提交数据到控制器MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!