我已经构建了一个页面,其中主页和详细信息(图片,aso)显示在一个页面上。但是,如果单击第二个表单的“提交”按钮,则如果第一个表单中的验证失败,则不会提交表单。如果我更正了值,则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")
}
控制器如下所示:
public ActionResult NewPic(int id, HttpPostedFileBase uploadFile)
{
// uploadFile is null
}
答案 0 :(得分:1)
您忘记在NewPic方法之前添加 [HttpPost] 。因此 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" }))
有关详细信息,请访问here