我有ComicPanel
的编辑页面,其中包含ComicPanelText
列表。
我想添加一个新的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 :(得分:0)
我猜您可以将按钮名称传递给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);
}