我有一个包含表单的部分视图“CreateJobLine.cshtml”。此表单在使用 @Html.Action 方法呈现时创建没有表单标记的表单字段。我使用了 Html.BeginForm 并对表单进行了硬编码(如下),但在这两种情况下它都不会生成标签。我还使用 jqueryui 的对话框小部件来显示表单。
---局部视图---
//filename: CreateJobLine.cshtml
@model Recruitment.Models.JobLine
@if(false){
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}
<div id="addnewjoblinediv">
<form id="createjoblineform" action="@Url.Action("CreateJobLine")" method="post">
@Html.ValidationSummary(true)
<fieldset>
<legend>JobLine</legend>
@Html.Hidden("job_id", Model.JobId)
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TimeSpent)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TimeSpent)
@Html.ValidationMessageFor(model => model.TimeSpent)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.JobId)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.JobId)
@Html.ValidationMessageFor(model => model.JobId)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
</form>
</div>
在编辑视图中,我调用了 Html.Action 来加载部分视图。
- -编辑视图 - -
//filename Edit.cshtml
...
@Html.Action("CreateJobLine", "Job", new { job_id = Model.Job.JobId})
<a id="addnewjoblinelink" href="#">Add New JobLine</a>
</fieldset>
}
<script type="text/javascript">
$(document).ready(function () {
$("div#addnewjoblinediv").dialog({ autoOpen: false, modal:true, width:550, title:"Add New JobLine"});
$("a#addnewjoblinelink").click(function () {
$("div#addnewjoblinediv").dialog("open");
}
);
}
);
</script>
这是控制器---作业控制器---
//JobController.cs
...
[HttpGet]
public PartialViewResult CreateJobLine(int job_id)
{
var jobline = new JobLine();
jobline.JobId = job_id;
jobline.TimeSpent = 0;
return PartialView(jobline);
}
[HttpPost]
public ActionResult CreateJobLine(JobLine jobline)
{
if (ModelState.IsValid)
{
db.JobLines.Add(jobline);
db.SaveChanges();
return RedirectToAction("Index");
}
return RedirectToAction("Edit", new { id = jobline.JobId });
}
...
调用 /Job/CreateJobLine/5 获取包含所有表单元素的表单内容,但缺少表单标签本身。我想将该表单操作设置为 /Job/CreateJobLine 发布方法,以便我可以创建一个 JobLine。
谢谢你。