[英]Add child to parent list inside MVC parent edit
I have an edit page for a ComicPanel
which contains a list of ComicPanelText
. 我有一个
ComicPanel
的编辑页面,其中包含ComicPanelText
列表。
I want to add a new ComicPanelText, so I need an 'Add' button, but the submit button goes to my EditController. 我想添加一个新的ComicPanelText,所以我需要一个“添加”按钮,但提交按钮将转到我的EditController。 How can I add a new submit button determine which button was pressed in the controller?
如何添加新的提交按钮来确定控制器中按下了哪个按钮?
Views/Shared/EditorTemplates/ComicPanelText.cshtml
查看/共享/ EditorTemplates / ComicPanelText.cshtml
@model ComicNet.Models.ComicPanelText
<tr>
<td></td>
<td>
@Html.HiddenFor(x => x.ComicPanelTextId)
@Html.TextBoxFor(x => x.ComicText)
</td>
</tr>
Views/ComicPanels/Edit.cshtml
查看/ 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
ComicPanelsController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(ComicPanel comicPanel)
{
//...
}
I guess you can pass the button name into the Controller 我想你可以将按钮名称传递给Controller
<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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.