MVC form moves when it is validated - asp.net-mvc

I have created an MVC form.
Everything is working fine, however, when error messages are displayed, they move form to the left.
This is a code:
<div style="text-align: center; padding-top: 40px;">
<h2 style="color: white;">Title</h2>
#using (Html.BeginForm())
{
<div>
#Html.EditorForModel()
<p><input type="submit" class="k-primary" value="Log In" /></p>
</div>
}
</div>
This is my Edit Template:
<div style="margin: 0px auto; text-align: center; display: table;">
<table>
<tr>
<td>#Html.EditorFor(x => x.UserName, new { htmlAttributes = new { #class = "form-control", placeholder = "username" } })</td>
<td>#Html.ValidationMessageFor(x => x.UserName,"", new { #class = "text-danger"})</td>
</tr>
<tr>
<td>#Html.EditorFor(x => x.Password, new { htmlAttributes = new { #class = "form-control", placeholder = "password" } })</td>
<td>#Html.ValidationMessageFor(x => x.Password)</td>
</tr>
<tr style="height:70px;">
<td colspan="2"><input type="submit" class="k-primary" value="Log In" /></td>
</tr>
</table>
</div>
This is the generated code:
he form as position:fixed. It did not fix it
What am I missing, so the form does not move anywhere and stays fixed?

Related

MVC - Model validation does not fire when using EditorTemplate with EditorForModel

I'm using EditorTemplate for Html.EditorForModel()
When used without EditorTemplate, validation works.
Without EditorTemplate - validation does not work.
This is my Form:
<div style="padding-top: 20px;">
#using (Html.BeginForm())
{
#Html.EditorForModel()
}
</div>
This is EditorTemplate:
<div style="margin: 0px auto; text-align: center; display: table;">
<table>
<tr>
<td>#Html.EditorFor(x => x.UserName, new { htmlAttributes = new { placeholder = "username" } })</td>
</tr>
<tr>
<td>#Html.EditorFor(x => x.Password, new { htmlAttributes = new { placeholder = "password" } })</td>
</tr>
<tr style="height:70px;">
<td><input type="submit" class="k-primary" value="Log In" /></td>
</tr>
</table>
</div>
This is html generated:
What am I missing?
I solved it by adding Html.ValidationMessageFor for both properties
#Html.ValidationMessageFor(x => x.UserName,"", new { #class = "text-danger"})

Get value of DropDown at Controller while submitting form MVC

This is My View.
#using (Html.BeginForm("uploadimage",
"PatientDocumentsMain",
FormMethod.Post,
new { #Area = "PatientDocument", enctype =
"multipart/form-data" }))
{
<table cellspacing="0" cellpadding="0" class="table table-
striped">
<tr>
<td>
Document Name:<span class="spnError">*</span>
</td>
<td>
<input type="text" id="txtDocumentName" name="DocName"
class="required form-control" />
</td>
</tr>
<tr>
<td class="tdEditDoc">
<span>Document Type:</span><span class="spnError">*</span>
</td>
<td id="tdDocumentCategory">
#Html.DropDownList("ddlDocumentCategory", null, new { #id = "",
#onchange = "AddCategory();", #class =
"required form-control", #name= "DocType" })
</td>
</tr>
<tr>
<td class="tdEditDoc">
<span>Date:</span><span class="spnError">*</span>
</td>
<td>
<input type="text" id="txtPatientDocumentDate" class="Date
required IsDate form-control" name="DocDate" />
</td>
</tr>
<tr>
<td class="tdEditDoc" style="height: 25px;">
<span>Confidental:</span>
</td>
<td>
<input type="checkbox" id="chkPatientDocumentIsConfedential"
/>
</td>
</tr>
<tr>
<td class="tdEditDoc" style="vertical-align: top">
Comments:
</td>
<td>
<textarea id="txtPatientDocumentComments" name="comments"
style="margin-right: 15px; width: 245px; height: 69px;
border-width: 1px; border-color: #c4c4c4;resize:none"
class="form-control">
</textarea>
</td>
</tr>
</table>
<input type="file" name="file" id="file" title="Upload file(s)" />
}
I'm submitting this form to this controller
public void uploadimage(string DocName, string DocType, string DocDate, string d, string comments, HttpPostedFileBase file)
{
}
I'm getting all other parameters except DropDown value.Plus How can I get Value of checkbox(Checked or not). I'm not using any model and want to do without it.
First parameter is the name of field
#Html.DropDownList("DocType", null, new { #id = "", #onchange = "AddCategory();", #class = "required form-control" })
Dropdown bind with null value ?
#Html.DropDownList("ddlDocumentCategory", null, new { #id = "", #onchange = "AddCategory();", #class = "required form-control", #name= "DocType" })
Also make sure name of input fields same as string parameter.
Helpful link
How to submit your form in asp.net mvc

How to pass value of radio buttom with Html.ActionLink to controller

How can i pass my radio value with action link to controller
this is my View Page code :
foreach (var item in Model)
{
<div style="height:20px; background-color:#e1e1e1; border-radius:5px; padding:5px">
<div style="float: left; margin-right: 10px">
#Html.RadioButton("DealerRadio", item.id, new { #style = "margin-right:10px" })
</div>
<div style="float: left; margin-right: 30px">
#Html.DisplayFor(modelItem => item.company, new { #style = "margin-right:30px" })
</div>
<div style="float: left; margin-right: 30px">
#Html.DisplayFor(modelItem => item.address, new { #style = "margin-right:10px" })
</div>
<div style="float:right">
#Html.Label("Tell:") #Html.DisplayFor(modelItem => item.contact)
</div>
</div>
<div class="clear"></div>
}
<p>#Html.ActionLink("BUY NOW", "PreOrder", new { Dealer_id = "", Tire_id = ViewBag.Tireid }, new { #class = "btn btn-Buy" })</p>
pleas help me to do this

MVC dropdownlist filter onchange

I have an index view with dropdownlist filter. Whenever a new item is selected from the dropdownlist, the index page will display data based on groupid. Here is the view
#using (Html.BeginForm("index", "service"))
{
<div class="row">
<div class="navbar navbar-default">
<div class="navbar-header" style="margin:10px 0px 5px 5px">
<div class="col-lg-12">
<a id="btnSAdd" class="btn btn-info glyphicon glyphicon-plus" href="/AppName/Service/Create"> Create New </a>
<button type="submit" id="btnSSave" class="btn btn-info glyphicon glyphicon-floppy-save"> Save </button>
</div>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse" style="margin:10px 0px 5px 5px">
<div class="input-group">
<span class="navbar" style="font-family:'Californian FB';font-weight:bold">Select a Menu:</span>
#Html.DropDownList("GroupID", new SelectList(ViewBag.TopMenuList, "GroupID", "GroupName"),
new { #class = "btn btn-info dropdown-toggle", #style = "border-color:grainsboro", #onchange = "window.location = 'service/index?groupid=' + this.value" })
</div>
</div>
</div>
</div>
<div class="row">
<div id="drag" class="panel-body">
<table class="table table-hover" style="width:auto" id="sTable">
<colgroup>
<col width="30" />
<col width="250" />
<col width="250" />
<col width="200" />
</colgroup>
<thead>
<tr>
<th> </th>
<th>Parent Group</th>
<th>Service Name</th>
<th>Service Description</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < Model.Count; i++)
{
<tr class="orderedrow">
<td class="rowhandler">
<div class="drag rowdip">#Html.HiddenFor(model => model[i].ServiceID)</div>
</td>
<td>#Html.DropDownListFor(model => model[i].GroupID, new SelectList(Model[i].ParentServiceGroupList, "GroupID", "GroupName", Model[i].GroupID))</td>
<td>#Html.EditorFor(model => model[i].ServiceName, new { htmlAttributes = new { #style = "width:250px" } })</td>
<td>#Html.EditorFor(model => model[i].ServiceDescription, new { htmlAttributes = new { #style = "width:250px" } })</td>
</tr>
}
</tbody>
</table>
</div><!--End Div drag-->
</div><!--End of row-->
}
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
The problem I am having is the filter works only the first time as
http://localhost/AppName/service/index?groupid=4
. The second time, it seems to append my url as
http://localhost/AppName/service/service/index?groupid=5
How do I fix this?
Thanks
Try to change this
#onchange = "window.location = 'service/index?groupid=' + this.value"
to this
#onchange = "window.location = '/service/index?groupid=' + this.value"

MVC 5 - Information in partial view is null after submit

I have a large form and it's becoming too big so I want to start using partial view to keep it cleaner.
Here is a little part of the form that shows the main contact and a list of alternative contacts. The user can add more contacts (the buttons shows a popup to insert new contact) and the user can change the contact from active to inactive (through the checkbox on the table). Right now this is working but like i said i want to be able to use a partial view for the table.
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Contacts</h3>
</div>
<div class="box-body">
<div class="form-group">
<div class="col-xs-4">
<label>Phone:</label>
#Html.EditorFor(model => model.Persons.Phone, new { htmlAttributes = new { #class = "form-control", #id = "phone" } })
#Html.ValidationMessageFor(model => model.Persons.Phone, "", new { #class = "text-danger" })
</div>
<div class="col-xs-8">
<label>Email:</label>
#Html.EditorFor(model => model.Persons.Email, new { htmlAttributes = new { #id = "email", #class = "form-control", #placeholder = "Introduza o email..." } })
#Html.ValidationMessageFor(model => model.Persons.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<label>Address:</label>
#Html.TextAreaFor(model => model.Persons.Address, new { #class = "form-control", #placeholder = "Introduza a morada...", #rows = 3 })
#Html.ValidationMessageFor(model => model.Persons.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
#if (Model.Contacts != null && Model.Contacts.Count > 0)
{
<table id="example2" class="table table-bordered table-hover dataTable" aria-describedby="example2_info">
<thead>
<tr role="row">
<th class="sorting_asc" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1" aria-sort="ascending">Contact</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Contact Type</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Active</th>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
#{
string cssClass = string.Empty;
string activo = string.Empty;
}
#for (var i = 0; i < Model.Contacts.Count; i++)
{
cssClass = i % 2 == 0 ? "even" : "odd";
<tr class="´#cssClass">
<td class=" ">#Html.DisplayFor(model => Model.Contacts[i].Contact)</td>
<td class=" ">#Html.DisplayFor(model => Model.Contacts[i].contacttypes.Name)</td>
<td class=" ">#Html.CheckBoxFor(model => Model.Contacts[i].IsActive, new { #class = "flat-green" })</td>
</tr>
#Html.HiddenFor(model => Model.Contacts[i].ContactsId)
}
</tbody>
</table>
}
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<input type="button" value="Add New Contact" class="buttonCreate btn btn-primary btn-sm" />
</div>
</div>
</div>
So here is the same HTML using the partial view
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">Contacts</h3>
</div>
<div class="box-body">
<div class="form-group">
<div class="col-xs-4">
<label>Phone:</label>
#Html.EditorFor(model => model.Persons.Phone, new { htmlAttributes = new { #class = "form-control", #id = "phone" } })
#Html.ValidationMessageFor(model => model.Persons.Phone, "", new { #class = "text-danger" })
</div>
<div class="col-xs-8">
<label>Email:</label>
#Html.EditorFor(model => model.Persons.Email, new { htmlAttributes = new { #id = "email", #class = "form-control", #placeholder = "Introduza o email..." } })
#Html.ValidationMessageFor(model => model.Persons.Email, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<label>Address:</label>
#Html.TextAreaFor(model => model.Persons.Address, new { #class = "form-control", #placeholder = "Introduza a morada...", #rows = 3 })
#Html.ValidationMessageFor(model => model.Persons.Address, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
#Html.Partial("ContactListControl", Model.Contacts)
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<input type="button" value="Add New Contact" class="buttonCreate btn btn-primary btn-sm" />
</div>
</div>
</div>
and here is the Partial view:
#model List<RecruitmentWeb.Models.contacts>
<table id="example2" class="table table-bordered table-hover dataTable" aria-describedby="example2_info">
<thead>
<tr role="row">
<th class="sorting_asc" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1" aria-sort="ascending">Contato</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Tipo de Contato</th>
<th class="sorting" role="columnheader" tabindex="0" aria-controls="example2" rowspan="1" colspan="1">Activo</th>
</thead>
<tbody role="alert" aria-live="polite" aria-relevant="all">
#{
string cssClass = string.Empty;
string activo = string.Empty;
}
#for (var i = 0; i < Model.Count; i++)
{
cssClass = i % 2 == 0 ? "even" : "odd";
<tr class="´#cssClass">
<td class=" ">#Html.DisplayFor(model => Model[i].Contact)</td>
<td class=" ">#Html.DisplayFor(model => Model[i].contacttypes.Name)</td>
<td class=" ">#Html.CheckBoxFor(model => Model[i].IsActive, new { #class = "flat-green" })</td>
</tr>
#Html.HiddenFor(model => Model[i].ContactsId)
}
</tbody>
The Problem
If the user changes a contact from active to inactive or vice-versa, when I submit the form the changes don't go through. In fact, the list is null and contains no information. It works if i don't use partial view.
So what I'm I missing?
You passing only a property of your model to the partial so the the hidden inputs you are generating have the name attribute name="[0].ContactsId", name="[1].ContactsId" etc. whereas they needs to be name="Contacts[0].ContactsId", name="Contacts[1].ContactsId" etc (ditto for the checkboxes).
Change the partial view model to the same as the main view (you haven't indicated what it is), and then pass the model as
#Html.Partial("ContactListControl", Model)
and adjust the Html helpers to suit. However I would recommend you consider using a custom EditorTemplate for RecruitmentWeb.Models.Contacts rather than a partial view for this.

Resources