In this Form used validation summary for errors.
View:-
<div class="col-md-12 table-bordered" style="padding: 20px; margin:10px;">
<div class="col-md-12">#Html.ValidationSummary(false,"Please Correct Following details :", new { #class = "text-danger" })</div>
<div class="col-md-12" style="padding:10px;">
<div class="col-md-6">
<div style="width:95%;">
#Html.LabelFor(Model => Model.ItemName, new { style = "font-weight:bold;", #maxlength = "100" })
#Html.TextBoxFor(Model => Model.ItemName)
<span class="text-danger"> #Html.ValidationMessageFor(Model => Model.ItemName) </span>
</div>
</div>
<div class="col-md-6">
<div style="width:75%;">
#Html.LabelFor(Model => Model.ActiveProductGroup, new { style = "font-weight:bold;" })
#Html.DropDownListFor(Model => Model.ProductGroupID, new SelectList(ViewBag.ActiveProductGroup, "Value", "Text"), "--Select Value--", new { style = "Width:95%" })
<span class="text-danger"> #Html.ValidationMessageFor(Model => Model.ActiveProductGroup) </span>
</div>
</div>
</div>
<div class="col-md-12" style="padding:10px;">
<div class="col-md-3">
<div style="width:70%;">
#Html.LabelFor(Model => Model.SequenceNumber, new { style = "font-weight:bold;" })
#Html.TextBoxFor(Model => Model.SequenceNumber, new { style = "text-align:right", #maxlength = "3" })
<span class="text-danger"> #Html.ValidationMessageFor(Model => Model.SequenceNumber) </span>
</div>
</div>
<div class="col-md-3">
<div style="width:50%;">
#Html.LabelFor(Model => Model.UnitPrice, new { style = "font-weight:bold;" })
#Html.TextBoxFor(Model => Model.UnitPrice, new { style = "text-align:right", #maxlength = "5" })
<span class="text-danger"> #Html.ValidationMessageFor(Model => Model.UnitPrice) </span>
</div>
</div>
<div class="col-md-3">
<div style="width:50%;">
#Html.LabelFor(Model => Model.Quantity, new { style = "font-weight:bold;" })
#Html.TextBoxFor(Model => Model.Quantity, new { style = "text-align:right", #maxlength = "5" })
<span class="text-danger"> #Html.ValidationMessageFor(Model => Model.Quantity) </span>
</div>
</div>
<div class="col-md-3">
<div style="width:80%;">
#Html.LabelFor(Model => Model.EstimatedDeliveryDays, new { style = "font-weight:bold;" })
#Html.TextBoxFor(Model => Model.EstimatedDeliveryDays, new { style = "text-align:right", #maxlength = "2" })
<span class="text-danger"> #Html.ValidationMessageFor(Model => Model.EstimatedDeliveryDays) </span>
</div>
</div>
</div>
</div>
In this used html.validation summary
In this Form it should show asterisk (*) sign below textbox and error in validation summary
how to achieve it?
Can't you use the Required attribute in your model?
Example:
public class ClassName{
[Required(ErrorMessage = "* Please enter Item Name")]
public string ItemName {get;set;}
}
Related
The file "Add.cshtml" is calling the partial view "_ManagePartial.cshtml" and passing the "Model""however the "Model " is not being picked up in the partial view.
When I include the code that's on the partial view into "Add.cshtml" everything works great.
Please advise what I''m doing wrong.
Error Message:
Click here to see the error message screenshot
File: _ManagePartial.cshtml
<div class="row">
<div class="col-md-4">
<div class="form-group">
#Html.LabelFor(model => model.UserName)
#Html.EditorFor(model => model.UserName, new { htmlAttributes = new { #class = "form-control input-lg" } })
#Html.ValidationMessageFor(model => model.UserName, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserPassword)
#Html.EditorFor(model => model.UserPassword, new { htmlAttributes = new { #class = "form-control input-lg" } })
#Html.ValidationMessageFor(model => model.UserPassword, "", new { #class = "text-danger" })
</div>
<div class="form-group"><button type="submit" class="btn btn-primary btn-lg">Submit</button></div>
</div>
<div class="col-md-4">
<div class="form-group">
#Html.LabelFor(model => model.UserEmail)
#Html.EditorFor(model => model.UserEmail, new { htmlAttributes = new { #class = "form-control input-lg" } })
#Html.ValidationMessageFor(model => model.UserEmail, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#Html.LabelFor(model => model.UserConfirmPassword)
<input type="password" class="form-control input-lg" name="UserConfirmPassword">
</div>
</div>
</div>
File: Add.cshtml
#model CardDistro.Models.AddUserViewModel
#{
ViewBag.Current = "Users#Index";
String Action = ViewBag.FormAction;
}
<h2 class="page-header">#ViewBag.Title</h2>
#using (Html.BeginForm(Action, "Users", FormMethod.Post, new { id = "SiteAddUserContainer" }))
{
<input type="hidden" name="UserID" value="#Model.UserID" />
if (ViewBag.Retval != null){
<div class="row"><div class="col-md-8"><div class="alert alert-danger">#ViewBag.Retval</div></div></div>
}
Html.RenderPartial("_ManagePartial", Model);
}
_ManagePartial.cshtml needs to identify the model by starting the partial view with
#model CardDistro.Models.AddUserViewModel
so the compiler knows what to do with model=>UserName
so i have this view with multiple same form like this
<div class="col-md-5">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Special Leave on Purposes Rules</h3>
</div>
<div class="panel-body">
#using (Html.BeginForm("Rule", "Admin", FormMethod.Post, new { #class = "form-horizontal", placeholder = ViewBag.maternity }))
{
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Set Max For</label>
<div class="col-sm-8">
#Html.DropDownListFor(
model => model.ID, (SelectList)ViewBag.special,
"--Select One--",
new
{ //anonymous type
#class = "form-control input-sm",
id= "inputEmail3"
})
</div>
#Html.ValidationMessageFor(m => m.ID, null, new { #class = "text-danger" })
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-4 control-label">Set Max</label>
<div class="col-sm-8">
#Html.TextBoxFor(model => model.Max, new { #Class = "form-control" })
</div>
#Html.ValidationMessageFor(m => m.Max, null, new { #class = "text-danger" })
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Save</button>
</div>
}
</div>
</div>
</div>
<div class="col-md-5">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Maternity Leave Rules</h3>
</div>
<div class="panel-body">
#using (Html.BeginForm("Rule", "Admin", FormMethod.Post, new { #class = "form-horizontal" }))
{
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Maximum Leave</label>
<div class="col-sm-8">
<div class="input-group">
#Html.TextBoxFor(model => model.Max, new { #Class = "form-control", placeholder = ViewBag.maternity })
<span class="input-group-addon">In Row</span>
#Html.HiddenFor(model => model.ID, new { Value = 2 })
</div>
</div>
#Html.ValidationMessageFor(m => m.Max, null, new { #class = "text-danger" })
</div>
<div class="form-group">
<button type="submit" name="submit" value="Annual" class="btn btn-success">Save</button>
</div>
}
</div>
</div>
</div>
The problem here is that when the form submitted with wrong value, the modelstate validated and return error message for ALL of the form like in this picture.
I want that the error message is only shown for the form i submit, is that possible? Thanks for your help guys
I am having some problems solving this. The thing is, this is the only place it doesn't work, everywhere else in my project it works just fine and it's all the same.
here is my link:
<td class="col-sm-1 col-lg-1">#Html.ActionLink("Edit", "EditGrowWeek", "FinishedProductionMaintanance", new { #id = Model.growWeek.GWID }, new { #data_toggle = "modal", #data_target = "#editGrowWeek" })</td>
my controller:
[HttpGet]
public ActionResult EditGrowWeek(int id)
{
viewModel.growWeek = _ProductionGrowWeekRepository.GetOne(id);
return PartialView(viewModel);
}
[HttpPost]
//[ValidateAntiForgeryToken]
public ActionResult EditGrowWeek([Bind(Prefix="growWeek")] ProductionGrowWeek growWeek)
{
var growWeeks = _ProductionGrowWeekRepository.GetAll(growWeek.GProfileNo);
foreach (var week in growWeeks)
{
if (ModelState.IsValid)
{
_ProductionGrowWeekRepository.Edit(week);
}
}
return RedirectToAction("IndexGrowProfiles");
}
and finally my view:
#model SISCOM.ViewModel.FinishedProductionMaintananceViewModel
#{
Layout = null;
}
<div class="modal fade in" id="editGrowWeek" tabindex="-1" role="dialog" aria-labelledby="MyModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dissmiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
#using (Html.BeginForm("EditGrowWeek", "FinishedProductionMaintanance", FormMethod.Post))
{
#*#Html.AntiForgeryToken()*#
<form id="form">
#Html.ValidationSummary(true)
#Html.HiddenFor(model => model.growWeek.GWID)
#Html.HiddenFor(model => model.growWeek.CutSupplierCode)
#Html.HiddenFor(model => model.growWeek.DelvyWk)
#Html.HiddenFor(model => model.growWeek.Description_ID_)
#Html.HiddenFor(model => model.growWeek.Description_Product_)
#Html.HiddenFor(model => model.growWeek.Despatch)
#Html.HiddenFor(model => model.growWeek.FinishedDuration)
#Html.HiddenFor(model => model.growWeek.FinishedSite)
#Html.HiddenFor(model => model.growWeek.GapUpHandSite)
#Html.HiddenFor(model => model.growWeek.GapUpMCSite)
#Html.HiddenFor(model => model.growWeek.GerminationDuration)
#Html.HiddenFor(model => model.growWeek.GerminationSite)
#Html.HiddenFor(model => model.growWeek.Germinator)
#Html.HiddenFor(model => model.growWeek.GerminatorDuration)
#Html.HiddenFor(model => model.growWeek.GerminatorSite)
#Html.HiddenFor(model => model.growWeek.GProfileNo)
#Html.HiddenFor(model => model.growWeek.GrowWks1)
#Html.HiddenFor(model => model.growWeek.GrowWks2)
#Html.HiddenFor(model => model.growWeek.GrowWks3)
#Html.HiddenFor(model => model.growWeek.GrowWks4)
#Html.HiddenFor(model => model.growWeek.GrowWks5)
#Html.HiddenFor(model => model.growWeek.OrderWK)
#Html.HiddenFor(model => model.growWeek.PreGapDuration)
#Html.HiddenFor(model => model.growWeek.PreGapSite)
#Html.HiddenFor(model => model.growWeek.PrickOut)
#Html.HiddenFor(model => model.growWeek.PrickOutSite)
#Html.HiddenFor(model => model.growWeek.Priority)
#Html.HiddenFor(model => model.growWeek.ProdGrp)
#Html.HiddenFor(model => model.growWeek.ProdSeries)
#Html.HiddenFor(model => model.growWeek.ProdSeriesDetail)
#Html.HiddenFor(model => model.growWeek.ProductionGrowProfile)
#Html.HiddenFor(model => model.growWeek.RangeUpdateFlag)
#Html.HiddenFor(model => model.growWeek.Rooting_GermAssess)
#Html.HiddenFor(model => model.growWeek.SiteGrown)
#Html.HiddenFor(model => model.growWeek.SiteRange)
#Html.HiddenFor(model => model.growWeek.SiteSown)
#Html.HiddenFor(model => model.growWeek.Topping)
#Html.HiddenFor(model => model.growWeek.ToppingCode)
#Html.HiddenFor(model => model.growWeek.Transplant)
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.WksToDesp, "Weeks to despatch")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.WksToDesp, null, new { #class = "form-control", #placeholder = "Weeks to despatch" })
#Html.ValidationMessageFor(p => p.growWeek.WksToDesp, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.GapUp, "Gap up")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.GapUp, null, new { #class = "form-control", #placeholder = "Gap up" })
#Html.ValidationMessageFor(p => p.growWeek.GapUp, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.Pinch1, "Pinch1")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.Pinch1, null, new { #class = "form-control", #placeholder = "Pinch1" })
#Html.ValidationMessageFor(p => p.growWeek.Pinch1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.Pinch2, "Pinch2")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.Pinch2, null, new { #class = "form-control", #placeholder = "Pinch2" })
#Html.ValidationMessageFor(p => p.growWeek.Pinch2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.Space1, "Space1")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.Space1, null, new { #class = "form-control", #placeholder = "Space1" })
#Html.ValidationMessageFor(p => p.growWeek.Space1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.Space2, "Space2")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.Space2, null, new { #class = "form-control", #placeholder = "Space2" })
#Html.ValidationMessageFor(p => p.growWeek.Space2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.Space3, "Space3")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.Space3, null, new { #class = "form-control", #placeholder = "Space3" })
#Html.ValidationMessageFor(p => p.growWeek.Space3, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="editor-label">
#Html.LabelFor(p => p.growWeek.Check1, "Check1")
</div>
<div class="editor-field">
#Html.TextBoxFor(p => p.growWeek.Check1, null, new { #class = "form-control", #placeholder = "Check1" })
#Html.ValidationMessageFor(p => p.growWeek.Check1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Edit" class="btn btn-default" />
</div>
</div>
</form>
}
</div>
</div>
</div>
</div>
I've been checking everywhere and this one doesn't make a call at all... any ideeas?
P.S. ... this might be connected so, on this page, (On the index that renders this partial) my footer is being rendered somewhere in the middle of my page in code..
This doesn't make a lot of sense but here it is. The problem was that everytime i generated values for that table it removed the partial view following it for some reason (if anyone can make sense of this behaviour and can explain please do.)
so, my index for this page used to look like this:
<div class="tab-content col-md-9 col-lg-9 growProfilePage">
<div class="col-md-12 col-lg-12">
<div class="col-md-4">
#{ Html.RenderPartial("ListGrowProfiles");}
</div>
<div class="col-md-8">
#{ Html.RenderPartial("ListGrowWeeks");}
#{ Html.RenderPartial("EditGrowWeek");}
</div>
</div>
</div>
now it looks like this:
<div class="tab-content col-md-9 col-lg-9 growProfilePage">
<div class="col-md-12 col-lg-12">
#{ Html.RenderPartial("EditGrowWeek");}
<div class="col-md-4">
#{ Html.RenderPartial("ListGrowProfiles");}
</div>
<div class="col-md-8">
#{ Html.RenderPartial("ListGrowWeeks");}
</div>
</div>
</div>
and it all works (curiously enough, on other pages it's still like before, the edit following the list and it works like that).
I want to use receive invoice for very next entry using session but it don't. Here I populate Receive InvoiceNo in create controller. please help me..
var rmaster = db.FactoryReceiveMaster.ToList();
List<SelectListItem> lstFactoryReceiveMaster = new List<SelectListItem>();
if (rmaster != null && rmaster.Count > 0)
{
foreach (var rInvoice in rmaster)
{
SelectListItem item = new SelectListItem { Text = rInvoice.RInvoiceNo, Value = rInvoice.Receive_ID.ToString(), Selected = false };
lstFactoryReceiveMaster.Add(item);
}
}
Session["lstFactoryReceiveMaster"] = lstFactoryReceiveMaster;
model.AssembleInvoice = lstFactoryReceiveMaster;
Create View....
#using Kendo.Mvc.UI;
#model MvcMahindraTwoWheelerERP.Models.FactoryProductAssembledViewModel
<div class="panel panel-info">
<div class="panel-heading">
<h3 style="font-family:cursive;font-size:large;"> Assemble Product - Entry Form</h3>
</div>
<div class="panel-body">
#using (Html.BeginForm("Create", "FactoryAssembleProduct", FormMethod.Post))
{
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-horizontal">
<div class="form-group">
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.Assembled_Date)
#(Html.Kendo().DatePickerFor(model => model.Assembled_Date).Format("dd/MMM/yyyy").HtmlAttributes(new { #style = "width:100%;"}))
#Html.ValidationMessageFor(model => model.Assembled_Date)
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.FactoryReceiveMaster.RInvoiceNo)
#(Html.Kendo().ComboBoxFor(model => model.FactoryReceiveMaster.Receive_ID).BindTo(Model.AssembleInvoice).HtmlAttributes(new {#style = "width:100%;"}))
#Html.ValidationMessageFor(model => model.FactoryReceiveMaster.Receive_ID)
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.Product.ProdCode)
#(Html.Kendo().ComboBoxFor(model => model.Product.ProductID).BindTo(Model.AssembleProducts).HtmlAttributes(new {id="ProductID", #style = "width:100%;"}).Filter(FilterType.Contains))
#Html.ValidationMessageFor(model => model.Product.ProductID)
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.Description)
#Html.TextBoxFor(model => model.Description, new { id="descriptipn", #class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.Chassis_No)
#Html.TextBoxFor(model => model.Chassis_No, new {#class = "form-control" })
#Html.ValidationMessageFor(model => model.Chassis_No)
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.Engine_No)
#Html.TextBoxFor(model => model.Engine_No, new {#class = "form-control" })
#Html.ValidationMessageFor(model => model.Engine_No)
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.Key_No)
#Html.TextBoxFor(model => model.Key_No, new {#class = "form-control" })
#Html.ValidationMessageFor(model => model.Key_No)
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
#Html.LabelFor(model => model.Remarks)
#Html.TextBoxFor(model => model.Remarks, new {#class = "form-control" })
#Html.ValidationMessageFor(model =>model.Remarks)
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Save Changes" />
</div>
</div>
</div>
</div>
}
</div>
Now, now how to fix it ...?
I am fairly new to MVC and a little stumped on this one.
I implemented ReCaptcha last night in my Umbraco MVC 4 application (6.1.6).
ReCaptcha works, the controller gets the correct captcha valid state and all is good. The problem is when the captcha is incorrect, I set the ModelState.AddModelError(string, string) and return the model in order to show that the captcha entered as incorrect, but the view never triggers the Html.ValidateMessage("captcha-error") to show the actual error message updated in the ModelState.
Any help or advice would be appreciated
Controller:
[HttpPost]
[RecaptchaControlMvc.CaptchaValidator]
public ActionResult SubmitContactFormAction(Models.ContactFormModel model, bool captchaValid, string captchaErrorMessage)
{
if (!captchaValid)
{
ModelState.AddModelError("captcha-error", captchaErrorMessage);
return View(model);
}
else
{
// Test ModelState and do stuff
}
}
View:
#model WebStaging.Models.ContactFormModel
#using Recaptcha
#using (Ajax.BeginForm("SubmitContactFormAction", "ContactForm", null, new AjaxOptions { HttpMethod = "POST" }, new { #id = "frmContactForm" }))
{
#Html.ValidationSummary(true)
<div>
Fields marked with * are required<br/><br />
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Name) *
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Name, new { #class = "input-242" })<br />
#Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Email) *
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Email, new { #class = "input-242" })
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Phone)
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Phone, new { #class = "input-242" })
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Subject) *
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Subject, new { #class = "input-242" })
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Message) *
</div>
<div class="input-column">
#Html.TextAreaFor(model => model.Message, new { #class = "input-textarea" })
</div>
</div>
<br />
<div id="captcha-panel" class="clear" style="text-align: right; float: right; padding-top: 5px;">
#Html.Raw(Html.GenerateCaptcha("captcha", "white"))
</div>
<p>
<div id="captcha-result" class="clear">
#Html.ValidationMessage("captcha-error")
</div>
<div class="clear pad-5" style="float: right; text-align: center;">
<button type="submit">Send</button><br />
<div id="submit-status" style="color: Green;"></div>
</div>
</div>
I think problem is caused because ReCaptcha is not a simple web control. I think you can easy walk around problem changing your "captcha-result" div with new class or style attribute.
<div id="captcha-result" class="someClassWithRedBigFont" >
#Html.ValidationMessage("captcha-error")
</div>
If you using jQuery you can check jQuery Validate plugin.