When pushing a button inside a partial view, parent form is validated - asp.net-mvc

In a MVC 5 application, I have a partial view with a button to submit some information. But when I push that button the parent form is validated, making all the validating errors displayed. All I want is that only the validated fields in the partial view are executed with no interaction with the parent form.
Code update (partial view, UploadFile):
<form action="#Url.Action("Upload","File")" method="post" enctype="multipart/form-data">
<label for="photo">Photo:</label>
<input type="file" name="photo" id="photo" />
<input type="submit" value="Upload" />
</form>
Code parent form:
<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>

Since you have a <form> tag inside the partial view and you put #Html.Partial("UploadFile") inside #using (Html.BeginForm()) block like below
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
....
....
....
#Html.Partial("UploadFile")
}
It will be rendered as nested forms like this
<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>
which is invalid html. You need to move #Html.Partial("UploadFile") outside of #using (Html.BeginForm()) block
<h2>Create</h2>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
....
....
....
}
#Html.Partial("UploadFile")

This may cause due to nested form.
Make sure that child form is not nested inside another form.
the HTML specs forbid nested forms.
#using (Html.BeginForm())
{
..
}
#Html.Partial("UploadFile") // place it outside of main form
As partial view contain another form you need to place it outside of main form anotherwise it will be nesting(HTML specs forbid nested forms)

Related

Creating an edit modal in ASP.NET - MVC

I have Index.cshtml with the code below
#model IEnumerable<Demo.Models.RequestList>
....
#foreach (var item in Model)
{
<td>#Html.DisplayFor(modelItem => item.Name)</td>
...
<a class="dropdown-item" onclick="edit(#item.ID)" href="#Url.Action("Edit", "Home", new { id = item.ID })">Edit</a>
}
It mean, I get data from DB and show on the table. Each row of the table I create a button for edit.
When I click Edit button, it will href="#Url.Action("Edit", "Home", new { id = item.ID })
But now, I want create Edit modal or a #html.partial on the Modal instead of href to a new page. How can I do that?
It is my Edit code.
#model Demo.Models.RequestList
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>RequestList</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ID)
<div class="form-group">
#Html.LabelFor(model => model.Code, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Code, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Code, "", new { #class = "text-danger" })
</div>
</div>
....
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>

How can I get value of EditorFor in ASP.Net Mvc and passing value in a button as parameter

I want pass some Id's values from my Edit form.
Let say I want pass value of ProductId, CountryId and ModelId and I tried like this:
<input type="submit" value="Save" class="btn btn-default" onclick="return Update(#(model.ProductId),#(model.CountryId),#(model.ModelId) )/>
But not working.
I'am in my Edit form and this is my form.
#model Products.Models.Product
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Product</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.ProductId)
#Html.HiddenFor(model => model.CountryId)
#Html.HiddenFor(model => model.ModelId)
<div class="form-group">
#Html.LabelFor(model => model.ProductName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Model, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Model, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Model, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Model, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Status, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Status, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Status, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" onclick="return Update(#(model.ProductId),#(model.CountryId),#(model.ModelId) )/>
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Thank you!
<input type="submit" value="Save" class="btn btn-default" onclick="return Update(#Model.ProductId,#Model.CountryId,#Model.ModelId)/> And you will get all three Values. Happy coding!

How do i pass the values from html.editorfor() to my controller

This is the code generated by the Visual Studio when i added a view for my Edit function
I want to pass the values inside the <input> generated by the #Html.EditorFor() from the view to my controller.
How can i access those values? Can i do it without manually declaring textboxes via <input class="form-control text-box single-line" id="Surname" name="Surname" type="text"> ?
#using (Html.BeginForm("Save","my",FormMethod.Post)){
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>User</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
#Html.HiddenFor(model => model.Id)
<div class="form-group">
#Html.LabelFor(model => model.Name, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Name, new { htmlAttributes = new { #Value=Model.Name, #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Name, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
UPDATE
found the answer, i simply needed to pass this code as an argument in my function inside the controller that would create an object User with properties inside Include
public string Submit ([Bind(Include = "Name")] User user){
}

How to reference multiple models in same MVC view [duplicate]

This question already has answers here:
how to create a view with multiple models mvc 4?
(2 answers)
Closed 7 years ago.
I've been trying to dissect an automated MVC view & controller, and modify it so that i can insert information from a form in the view, on to two separate database tables (Entity framework database first).
here's what i have so far. it works if i reference either of the namespaces at the top on their own, but not together?
#model manage.mysite.DataModels.Property_Type
#model manage.mysite.DataModels.Room_Type
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
<div class="form-horizontal">
<h4>Property_Type</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.PropertyType, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PropertyType, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PropertyType, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RoomType, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RoomType, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RoomType, "", 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>
</div>
}
It is not possible to reference several models in a View.
However, you can create composite model, which has the two other models as properties.
As such:
using manage.mysite.DataModels;
namespace manage.mysite.ViewModels
{
public class FooViewModel
{
public Property_Type PropertyType { get; set; }
public Room_Type RoomType { get; set; }
}
}
And then serve this model instead.
#model manage.mysite.ViewModels.FooViewModel
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm())
{
<div class="form-horizontal">
<h4>Property_Type</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.PropertyType.PropertyType, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PropertyType.PropertyType, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PropertyType.PropertyType, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.RoomType.RoomType, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.RoomType.RoomType, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.RoomType.RoomType, "", 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>
</div>
}
See also:
MVC Multiple Models in One View
how to create a view with multiple models mvc 4?
Which is the best way to use multiple models with sames properties and using a unique view?

How change name in TextBoxFor in MVC?

I want to add dynamicly new partial form on page. I used this code:
Guid index = Guid.NewGuid();
<input type="hidden" name="People.index" value="#index" />
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.surname, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.surname, new { name = "Author[index].surname", htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.surname, "", new { #class = "text-danger" })
</div>
</div>
But in source code after page loading i see this:
<div class="form-group">
<label class="control-label col-md-2" for="surname">Фамилия</label>
<div class="col-md-10">
<input htmlAttributes="{ class = form-control }" id="surname" name="surname" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="surname" data-valmsg-replace="true"></span>
</div>
</div>
How i can enabled Guid in name field? And how change fields in TextBoxFor?

Resources