Asp.net mvc 在局部视图中按下按钮时,将验证父窗体
在MVC5应用程序中,我有一个带有按钮的局部视图来提交一些信息。但当我按下该按钮时,父窗体将被验证,从而显示所有验证错误。我想要的是,只执行部分视图中的已验证字段,而不与父窗体交互 代码更新(部分视图,上载文件):Asp.net mvc 在局部视图中按下按钮时,将验证父窗体,asp.net-mvc,validation,asp.net-mvc-5,Asp.net Mvc,Validation,Asp.net Mvc 5,在MVC5应用程序中,我有一个带有按钮的局部视图来提交一些信息。但当我按下该按钮时,父窗体将被验证,从而显示所有验证错误。我想要的是,只执行部分视图中的已验证字段,而不与父窗体交互 代码更新(部分视图,上载文件): 照片: 代码父窗体: <h2>Create</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal">
照片:
代码父窗体:
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CategoriaId, "CategoriaId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CategoriaId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoriaId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Titulo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Titulo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Titulo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Descripcion, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Descripcion, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Descripcion, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Precio, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Precio, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Precio, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
@Html.Partial("UploadFile")
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
创建
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.CategoriaId,“CategoriaId”,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownList(“categorizaid”,null,htmlAttributes:new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.CategoriaId,“,new{@class=“text danger”})
@LabelFor(model=>model.Titulo,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Titulo,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Titulo,“,new{@class=“text danger”})
@LabelFor(model=>model.description,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.description,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.description,“,new{@class=“text danger”})
@LabelFor(model=>model.Precio,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Precio,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Precio,“,new{@class=“text danger”})
@Html.Partial(“上传文件”)
}
@ActionLink(“返回列表”、“索引”)
这可能由于嵌套形式而导致
确保子窗体未嵌套在其他窗体中
HTML规范禁止嵌套表单
@using (Html.BeginForm())
{
..
}
@Html.Partial("UploadFile") // place it outside of main form
由于部分视图包含另一个表单,您需要将其放置在主表单之外,否则它将嵌套(HTML规范禁止嵌套表单),因为您在部分视图中有一个
标记,并且您使用(HTML.BeginForm())块将@HTML.partial(“上载文件”)
放置在@内部,如下所示
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
....
....
....
@Html.Partial("UploadFile")
}
请出示你的代码,如果没有看到你的代码,很难回答。我已经插入了代码。你的嵌套表单是无效的HTML。这就是问题所在。我刚开始用MVC编程,所以我用标准ASP.NET编程。谢谢大家。
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
....
....
....
@Html.Partial("UploadFile")
}
<h2>Create</h2>
<form>
....
....
....
<form action="/File/Upload" method="post" enctype="multipart/form-data">
<label for="photo">Photo:</label>
<input type="file" name="photo" id="photo" />
<input type="submit" value="Upload" />
</form>
</form>
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
....
....
....
}
@Html.Partial("UploadFile")