2017-04-15 11 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); 
     } 
にボタン名を渡すことができますね
関連する問題