我目前正致力于插入MVC 5应用程序的记录功能。由于某种原因,后期行动方法不会触发。我在View上创建了一个按钮,该按钮应该触发post动作方法。不知道为什么它没有开火。表单元素在局部视图中,按钮控件不是托管局部视图的主视图。
这是代码
主视图
@model CC.GRP.MCRequest.ViewModels.NewRequestViewModel
<div class="newrequest">
@Html.Partial("~/Views/Request/RequestHeaderView.cshtml");
</div>
<div id="buttonalign" class="buttonalign" >
<button type="button" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Request_Insert", "Request")'" >Save as draft</button>
</div>
请求标题视图
@model CC.GRP.MCRequest.ViewModels.NewRequestViewModel
@using (Html.BeginForm())
{
<div id="NewRequest">
<div class="row">
<div class="col-md-4">
<div class="form-group">
@Html.LabelFor(model => model.RequestID, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
<div class="editor-field">
@Html.EditorFor(model => model.RequestID, new { htmlAttributes = new { @class = "form-control", style = "width:100%", @readonly = "readonly" } })
</div>
@Html.ValidationMessageFor(model => model.RequestID, "", new { @class = "text-danger" })
</div>
</div>
<div class="clearfix"></div>
<div class="form-group">
@Html.LabelFor(model => model.Name1, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
<div class="editor-field">
@(Html.Kendo().ComboBoxFor(model => model.Name1)
.HtmlAttributes(new { style = "width:100%" })
.DataTextField("Name1")
.DataValueField("CustomerMasterDataId")
.DataSource(dataSource => dataSource
.Read(read => read.Action("RequestHeader_CustomerData", "Request").Type(HttpVerbs.Post))
)
.Events(e =>
{
e.Change("onCustomerComboChange");
})
)
</div>
@Html.ValidationMessageFor(model => model.CustomerNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="clearfix"></div>
<div class="form-group">
@Html.LabelFor(model => model.CustomerNumber, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-6">
<div class="editor-field">
@Html.EditorFor(model => model.CustomerNumber, new { htmlAttributes = new { @class = "form-control", style = "width:100%", @readonly = "readonly" } })
</div>
@Html.ValidationMessageFor(model => model.CustomerNumber, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
请求控制器
[HttpPost]
public ActionResult Request_Insert(NewRequestViewModel userVM)
{
NewRequestViewModel newReqeustViewModel = new NewRequestViewModel();
if (!ModelState.IsValid)
{
return null;
}
requestRepository.CreateRequest(Mapper.Map<Request>(userVM));
//return Json(requestRepository.CreateRequest(userVM));
return View("NewRequestView", newReqeustViewModel);
}
答案 0 :(得分:2)
你应该把
<div id="buttonalign" class="buttonalign" >
<button type="button" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-default" onclick="location.href='@Url.Action("Request_Insert", "Request")'" >Save as draft</button>
</div>
内
@using (Html.BeginForm("Request_Insert", "Request", FormMethod.Post))
{
}
请求标题视图 中的
和 更新
<button type="button" class="btn btn-primary">Submit</button>
到
<button type="submit" class="btn btn-primary">Submit</button>