【发布时间】:2016-09-21 13:48:10
【问题描述】:
我的应用程序需要 CRUD 合同,我可以将文件附加到每个合同。 所以,在我的编辑/更新页面中,我有三种形式:
- 一个更新合同属性(Edit.cshtml)
- 向合同中添加文档 (AddDocument.cshtml)
- 从合同中删除文件(无需出示)
它看起来像这样:
编辑.cshtml
@model ContractViewModel
@Html.Action("AddDocument", "Contracts", new { id = Model.IdContract })
@Html.Action("RemoveDocument", "Contracts", new { id = Model.IdContract })
@using (Html.BeginForm("Edit", "Contracts", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.IdContract)
<div class="form-group">
@Html.LabelFor(model => model.ContractNumber, htmlAttributes: new { @class = "control-label col-md-4" })
<div class="col-md-8">
@Html.EditorFor(model => model.ContractNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ContractNumber)
</div>
</div> [...]
<input type="submit" value="Update"/>
}
AddDocument.cshtml
@model DocumentViewModel
@using (Html.BeginForm("AddDocument","Contracts", FormMethod.Post, new { @class = "form-horizontal", enctype="multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.IdContract)
<div class="form-group">
@Html.LabelFor(model => model.DocHttp, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(x => x.DocHttp, htmlAttributes: new { @class = "form-control", data_style = "btn-primary", type = "file", multiple = "multiple" })
@Html.ValidationMessageFor(model => model.DocHttp)
</div>
</div>
<input type="submit" value="Add"/>
}
ContractController.cs
public ActionResult Edit(int? id)
{
if (id == null)
{
throw new HttpException(400, "Bad request");
}
Contract contract = business.Get<Contract>(x => x.IdContract == id);
ContractViewModel vm = new ContractViewModel(contract);
if (contract == null)
{
throw new HttpException(404, "Not found");
}
return View(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ContractViewModel vm)
{
Contract contract = business.Get<Contract>(x => x.IdContract == id);
if (ModelState.IsValid)
{
[...]
}
return View(vm);
}
public ActionResult AddDocument(int id)
{
DocumentViewModel vm = new DocumentViewModel();
vm.IdContract = id;
return PartialView(vm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddDocument(DocumentViewModel vm)
{
Contract contract = business.Get<Contract>(x => x.IdContract == vm.IdContract);
if (ModelState.IsValid)
{
[...]
}
return RedirectToAction("Edit", "Contracts", new { id = vm.IdContract });
//return View(vm);
}
首先,问题是,当我提交Edit表单的时候,自然调用了[HttpPost]Edit方法,同时也调用了[HttpPost]AddDocument。是不是因为使用了 Html.Action 而不是 Html.RenderPartial?
如果我是对的,当您必须在生成局部视图之前进行处理时调用 Html.Action,而 Html.RenderPartial 只传递参数。
为什么要调用 [HttpPost]AddDocument 方法?谁叫它的?
其次,要绕过这个问题,我必须重定向到编辑页面,而不是调用 View 方法。但是,我丢失了输入的数据。我该如何解决这个问题?
谢谢。
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-partialview