c# - Model Error Not Showing after Http Post MVC 4 -
i validating date input server side , adding modelerror
if user input invalid. following code
public actionresult editoffer() { var offerid = convert.toint64(request.requestcontext.routedata.values["id"]); using (joyryde_storeentities context = new joyryde_storeentities()) { var objoffer = context.tbl_offer.where(x => x.lng_offer_id == offerid).firstordefault(); viewbag.offerid = offerid; viewbag.header = "edit " + objoffer.txt_offer_title; viewbag.actiontoperform = "edit"; if (system.io.file.exists(server.mappath(string.format("~/assets/images/stores/{0}/o_{1}_small.jpg", session["storeid"], offerid)))) { objoffer.txt_offer_small_path = string.format("~/assets/images/stores/{0}/o_{1}_small.jpg", session["storeid"], offerid); } return view("addoffer", objoffer); } } [httppost] public actionresult editoffer(tbl_offer modal, string add, string edit) { if (modelstate.isvalid) { using (joyryde_storeentities context = new joyryde_storeentities()) { var offerid = convert.toint64(request.requestcontext.routedata.values["id"]); if (!isofferexist(modal.dat_start_offer.value.date, modal.dat_end_offer.value.date.addhours(23).addminutes(59).addseconds(59).addmilliseconds(999), convert.toint64(session["storeid"]), offerid, add, edit, context)) { // code return redirecttoaction("alloffers", "store"); } else { modelstate.addmodelerror("dat_start_offer", "date not available"); // here adding modal error date if (edit != null) { return redirecttoaction("editoffer"); } else { return redirecttoaction("addoffer"); } } } } else { return redirecttoaction("editoffer"); }
view
<div class="panel-body container-fluid"> @using (html.beginform("editoffer", "store", formmethod.post, new { @class = "form-horizontal", enctype = "multipart/form-data" , id="offerform"})) { @html.antiforgerytoken(); @html.validationsummary(true); <div class="form-group"> <label class="col-sm-3 control-label">offer title</label> <div class="col-sm-6"> @html.textboxfor(model => model.txt_offer_title, new { @class = "form-control", placeholder = "offer title", autocomplete = "off", name = "title" }) @html.validationmessagefor(model => model.txt_offer_title, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">offer banner</label> <div class="col-sm-9"> <div class="image-container" style=" border: 1px solid #ccc; display: table;position:relative"> <a href="#editimage" data-toggle="modal" class="btn btn-sm btn-icon btn-inverse btn-round btn-image-edit" data-toggle="tooltip" data-original-title="edit"> <i class="icon wb-pencil" aria-hidden="true"></i> </a> <div class="img-preview preview-lg"> <img id="image_upload_preview" src="@url.content(string.format("~/assets/images/stores/{0}/o_{1}_small.jpg", session["storeid"], viewbag.offerid))" style="width:100%" alt="your image" /> </div> </div> <div class="input-group-file" style="margin-top:5px"> @html.textboxfor(modal => modal.txt_offer_small_path, new { @class = "hide", @readonly = "true", width = "0", id = "filepath" }) @html.validationmessagefor(modal => modal.txt_offer_small_path, "", new { @class = "text-danger" }) <span class=""> <span class="btn btn-success btn-small btn-file"> upload image <i class="icon wb-upload" aria-hidden="true"></i> <input type="file" name="files" accept="image/*" multiple="" id="fileupload" onchange="showimagepreview(this)"> </span> </span> </div> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">offer detail </label> <div class="col-sm-6"> @html.textareafor(model => model.txt_offer_text, new { @class = "form-control", placeholder = "offer text", autocomplete = "off", name = "text" }) @html.validationmessagefor(model => model.txt_offer_text, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">valid for</label> <div class="col-sm-4"> <div class="input-daterange" data-plugin="datepicker"> <div class="input-group"> <span class="input-group-addon"> <i class="icon wb-calendar" aria-hidden="true"></i> </span> @html.textboxfor(model => model.dat_start_offer, "{0:dd mmmm yyyy}", new { @class = "form-control from_date", placeholder = "start date", autocomplete = "off", name = "start" }) @html.validationmessagefor(model => model.dat_start_offer, "", new { @class = "text-danger" }) </div> <div class="input-group"> <span class="input-group-addon">to</span> @html.textboxfor(model => model.dat_end_offer, "{0:dd mmmm yyyy}", new { @class = "form-control to_date", placeholder = "end date", autocomplete = "off", name = "end" }) @html.validationmessagefor(model => model.dat_end_offer, "", new { @class = "text-danger" }) </div> </div> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label">is premium</label> <div class="col-sm-4"> <div class="radio-custom radio-default radio-inline"> @html.radiobuttonfor(model => model.int_is_prime, 1, new { @id = "ispremiumyes", name = "ispremium", @checked = "checked" }) <label for="ispremiumyes">yes</label> </div> <div class="radio-custom radio-default radio-inline"> @html.radiobuttonfor(model => model.int_is_prime, 0, new { @id = "ispremiumno", name = "ispremium", }) <label for="ispremiumno">no</label> </div> </div> </div> <div class="form-group"> <div class="col-sm-6 col-sm-offset-3"> <button type="submit" name="@viewbag.actiontoperform" class="btn btn-primary">submit </button> <button type="reset" class="btn btn-default btn-outline">reset</button> </div> </div> @html.hidden("cropwidth", new { id = "cropwidth" }) @html.hidden("cropheight", new { id = "cropheight" }) @html.hidden("croppointx", new { id = "croppointx" }) @html.hidden("croppointy", new { id = "croppointy" }) @html.hidden("imgsrc", new { id = "imgsrc" }) } <div class="modal fade" id="editimage" aria-labelledby="modallabel" role="dialog" tabindex="-1"> <div class="modal-dialog" role="document" style="width:1024px;height:768px"> <div class="modal-content "> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="modallabel">crop image</h4> </div> <div class="modal-body"> <div class="row"> <div class="col-sm-9"> <div class="cropper text-center"> <img id="image" src="@url.content(string.format("~/assets/images/stores/{0}/o_{1}_small.jpg", session["storeid"], viewbag.offerid))" style="max-width:730px;" alt="picture"> </div> </div> <div class="col-sm-3"> <div class="docs-preview clearfix"> <div class="img-preview preview-lg"></div> </div> </div> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> </div> </div> </div> </div> </div> </div> </div>
but modal error not showing on view. cause ?
when set model error use redirecttoaction, mean load new page, check in debug, after code go method , reloaded. have return view model.
modelstate.addmodelerror("dat_start_offer", "date not available"); if (edit != null) { return view(modal); //if model object named modal }
Comments
Post a Comment