C# 在MVC页面中获取第二个表单的输入
我已经建立了一个页面,其中主控和详细信息(图片,aso)显示在一个页面上。但是,如果单击了第二个表单的submitt按钮,则如果第一个表单中的验证失败,则不会提交该表单。如果我更正了这些值,C# 在MVC页面中获取第二个表单的输入,c#,asp.net-mvc,C#,Asp.net Mvc,我已经建立了一个页面,其中主控和详细信息(图片,aso)显示在一个页面上。但是,如果单击了第二个表单的submitt按钮,则如果第一个表单中的验证失败,则不会提交该表单。如果我更正了这些值,HttpPostedFileBase uploadFile为空 页面如下所示: @model app1.Models.MasterModel @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } @us
HttpPostedFileBase uploadFile
为空
页面如下所示:
@model app1.Models.MasterModel
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm(new { @class = "form-inline col-lg-12" }))
{
@Html.AntiForgeryToken()
<div>
<h4>MasterModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="row">
@*Master properties*@
<div class="col-md-4 col-lg-4">
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-8">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
@* aso... *@
</div>
</div>
}
@* Master Details *@
<div class="col-md-4 col-lg-4">
@using (Html.BeginForm("NewPic", "Master", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input name="uploadFile" type="file" />
<input type="submit" value="Upload File" /> <!-- First Button, does not work -->
<div class="container-fluid">
@foreach (app1.Models.PicModel b in Model.Pics)
{
var base64 = Convert.ToBase64String(b.DbPic);
var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
<img src="@imgSrc" width="200" height="200" />
}
</div>
@Html.ActionLink("Upload", "NewPic", new { id = Model.Id }) <!-- Second Button, does not work either -->
<label class="control-label col-md-4 col-lg-4" for="Title">Picer</label>
}
</div>
</div>
<div>
<div class="form-group">
<div class="col-md-offset-2 col-md-12 col-lg-12">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
您忘记将[HttpPost]放在NewPic方法之前。因此NewPic方法将被视为[HttpGet],因此它将不起作用
[HttpPost]
public ActionResult NewPic(int id, HttpPostedFileBase uploadFile)
{
// uploadFile is null
}
并且还为这两个表单提供适当的Id,如下所示,以便在客户端验证时使用这两个表单都很容易
表格1
@using (Html.BeginForm(new {id = "Form1", @class = "form-inline col-lg-12" }))
表格2
@using (Html.BeginForm("NewPic", "Master", FormMethod.Post, new { id = "Form2", enctype = "multipart/form-data" }))
有关详细信息,请访问您有一个嵌套表单。您正在单击哪个提交按钮?将它们拆分为两个局部视图,这可能有助于解决您的问题,这将有助于使您的代码更清晰尝试使用ajax表单,这将对第二个表单更有帮助
@using (Html.BeginForm("NewPic", "Master", FormMethod.Post, new { id = "Form2", enctype = "multipart/form-data" }))