簡體   English   中英

MVC5-發布包含Html.Action的視圖

[英]MVC5 - Posting a view which contains Html.Action

我的應用程序需要CRUD合同,並且我可以將文檔附加到每個合同中。 因此,在我的“編輯/更新”頁面中,有以下三種形式:

  • 一種更新合同屬性(Edit.cshtml)
  • 一個將文檔添加到合同中的文檔(AddDocument.cshtml)
  • 從合同中刪除文件的人(不必顯示)

它看起來像這樣:

Edit.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方法。 但是,我丟失了輸入的數據。 我該如何解決這個問題?

謝謝。

[...]以及[HttpPost] AddDocument。 這是由於使用了Html.Action而不是Html.RenderPartial嗎?

一個視圖中可以有多個表單,每個表單將僅調用對應的控制器方法。

如果我是對的,則在生成局部視圖之前必須進行處理時會調用Html.Action,而Html.RenderPartial只會傳遞參數。

檢查這篇文章

基本上,當調用@ html.Partial時,您將在沒有控制器調用的情況下導入html。 如果該部分視圖是強類型的,則需要確保進行調用的視圖的當前模型具有該部分視圖所需的模型。

因為您的局部視圖與調用視圖模型的模型不同,所以有兩種選擇:

1-與您的解決方案相同,調用action方法並為該視圖創建模型

2-您傳遞給調用@ Html.renderPartial或@ Html.Partial的視圖的模型必須在partial中包含所需的模型。 用法示例@Html.Partial("SomePartialview",Model.thePartialViewModel)

其次,要繞過該問題,我必須重定向到“編輯”頁面,而不是調用View方法。 但是,我丟失了輸入的數據。 我該如何解決這個問題?

數據在兩次重定向之間不會保留。

你可以做這個和/或閱讀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM