2017-04-15 92 views
0

我有一個ComicPanel的編輯頁面,其中包含ComicPanelText的列表。將子項添加到MVC父項的父項列表中編輯

我想添加一個新的ComicPanelText,所以我需要一個'添加'按鈕,但提交按鈕會轉到我的EditController。我怎樣才能添加一個新的提交按鈕來確定控制器中按下了哪個按鈕?

查看/共享/ EditorTemplates/ComicPanelText.cshtml

@model ComicNet.Models.ComicPanelText 

    <tr> 
     <td></td> 
     <td> 
      @Html.HiddenFor(x => x.ComicPanelTextId) 
      @Html.TextBoxFor(x => x.ComicText) 
     </td> 
    </tr> 

查看/ ComicPanels/Edit.cshtml

@model ComicNet.Models.ComicPanel 

@{ 
    ViewBag.Title = "Edit"; 
} 

<h2>Edit</h2> 

<form action="" method="post" enctype="multipart/form-data"> 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true); 

    <table> 
     <tr> 
      <td> 
       @Html.LabelFor(m => m.Name) 
      </td> 
      <td> 
       @Html.EditorFor(m => m.Name) 
      </td> 
     </tr> 

     <tr> 
      <td> 
       @Html.LabelFor(m => m.PanelNumber) 
      </td> 
      <td> 
       @Html.EditorFor(m => m.PanelNumber) 
      </td> 
     </tr> 
     <tr> 
      <td></td> 
      <td> 
       <img src='@Html.DisplayFor(model => model.Url)' width="200" height="200" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       @Html.LabelFor(m => m.Width) 
      </td> 
      <td> 
       @Html.EditorFor(m => m.Width) 
      </td> 
     </tr> 
     <tr> 
      <td> 
       @Html.LabelFor(m => m.Height) 
      </td> 
      <td> 
       @Html.EditorFor(m => m.Height) 
      </td> 
     </tr> 
     <tr> 
      <td> 
       @Html.LabelFor(m => m.ImageUpload) 
      </td> 
      <td> 
       @Html.TextBoxFor(m => m.ImageUpload, new { type = "file" }) 
      </td> 
     </tr> 

     @Html.EditorFor(x => x.ComicPanelTexts) 

    </table> 


    <div> 
     @Html.HiddenFor(m => m.FileName) 
    </div> 

    <div> 
     @Html.HiddenFor(m => m.ComicPanelId) 
    </div> 





    <button type="submit">Update</button> 
</form> 





<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

ComicPanelsController.cs

[HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Edit(ComicPanel comicPanel) 
     { 
//... 
} 

回答

0

我想你可以按鈕名稱傳遞到控制器

<input name="submit" type="submit" id="addText" value="Add" /> 



    public ActionResult Edit(ComicPanel comicPanel, string submit) 
    { 
     if(submit == "Add") 
     { 
      var newComicPanelText = new ComicPanelText(); 
      comicPanel.ComicPanelTexts.Add(newComicPanelText); 
      db.Entry(newComicPanelText).State = EntityState.Added; 
      db.Entry(comicPanel).State = EntityState.Modified; 
      db.SaveChanges(); 
      return View(comicPanel); 
     } 
相關問題