I have the following view,, which create 10 ajax.beginform ,, But the problem that i am facing is that incase an error occurs during the creation of the object then the ModelState.AddModelError will not be shown on the view although i have set the
returnContent("Addedd Succsfully"); } } catch(DbUpdateException) { ModelState.AddModelError(string.Empty,"The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
}//code goes here...
So how i can show the ModelState.AddModelError on my view. BR
I think to solve this issue you have to move all you form controls including the validation summary into a partial view and call that partial view in your view inside the form.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateAll(VisitLabResult vlr, int visitid )
{
try
{
if (ModelState.IsValid)
{
vlr.VisitID = visitid;
repository.AddVisitLabResult(vlr);
repository.Save();
return view("CreateAllPArtial",vlr);
}
}
catch (DbUpdateException)
{
JsonRequestBehavior.AllowGet);
ModelState.AddModelError(string.Empty, "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
return view("CreateAllPArtial",vlr);
}
My code might look stupid, but my intention is to convey the idea!
Cheers,
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
you are using an ajax form that replaces the html of a node with the server response. if there is an error, then you need to have the error message in that response, currently you have none.
you coudl turn your form into a partial view, that is used on the main page, and returned by the post action.
thanks a lot for your help, i have updated my view and action method as shown below. Now
i am returning a partial view in case of success or failure,, things are working fine
except that when i am returning a partial view from my action method the partial view table columns will be displayed below each other instead of being displayed beside each other(since they are table columns at the same table row) so they
look out of content,, so how i can fix this user interface issue.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateAll(VisitLabResult vlr, int visitid )
{
try
{
if (ModelState.IsValid)
{
var v = repository.GetVisit(visitid);
if (!(v.EligableToStart(User.Identity.Name)))
{ return View("NotFound"); }
vlr.VisitID = visitid;
repository.AddVisitLabResult(vlr);
repository.Save();
ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID);
// return Json(new { IsSuccess = "True" }, JsonRequestBehavior.AllowGet);
return PartialView("_create",vlr);
}
}
catch (DbUpdateException)
{
// return Json(new { IsSuccess = "False", description = "" }, JsonRequestBehavior.AllowGet);
ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID);
ModelState.AddModelError(string.Empty, "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests");
return PartialView("_create", vlr);
I tried the above on firefox and chrome and the table column were displyed correctly,, but on IE the problem still there
I don't see an opeing <table> tag in your view. Also, check that the <tr> tags are remaing in the rendered output. I think the replace ajax option might replace the entire element including the <tr>. If so just put <tr> tags in your partial.
thanks for the reply ,i tried to put <tr> in the partial view but it did not work also,
beside this i have an opening <table> tag , but i have only provided part of my view code to ease understanding my problem. here is my full view code:-
@model Medical.Models.VisitLabResult
@{
ViewBag.Title = "Create";
}
<h2>Create @ViewBag.visitid</h2>
<table>
<tr>
<th>
Lab Test
</th>
<th>
Result
</th>
<th>
Date Taken
</th>
<th>
Comment
</th>
<th>
</th>
<th>
</th>
</tr>
@for (int item = 0; item < 10; item++)
{
using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = item.ToString(),
InsertionMode = InsertionMode.Replace,
OnSuccess = string.Format(
"disableform({0})",
Json.Encode(item)),
}))
{
@* <td id = "@(item.ToString() + "td")" style="color: #7fb067; font-weight: bold">
</td>*@
<tr id = "@item">
@Html.Partial("_create",Model)
</tr>
}
}
</table>
<div>
@Html.ActionLink("Back To Current Visit", "Edit", "Visit", new { id = ViewBag.visitid }, null)
</div>
I think the problem is the way sections are rendered when they are put in partial views. I think your script sections in the partial view is not being put into the rendersection block in your layout/view. Try moving all your script into another partial view
and render that partial view where ever you want it to be rendered.
The sections doesnt work if your rendersection is defined in your layout, as the partial view doesnt have layout defined, only your view has the layout defined. So section defined in your view will get rendered in the rendersection block. Dont ask me why
!, i have seen this behaviour in razor.
Hope i am hitting the right target!
Cheers,
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
johnjohn1231...
Participant
922 Points
871 Posts
ModelState.AddModelError is not being displayed inside my view
May 02, 2012 12:05 PM|LINK
I have the following view,, which create 10 ajax.beginform ,, But the problem that i am facing is that incase an error occurs during the creation of the object then the ModelState.AddModelError will not be shown on the view although i have set the
The view looks as followAnd my action method which defines the ModelState.AddModelError looks as follow:-
So how i can show the ModelState.AddModelError on my view. BR
</div>gopakumar.r
Participant
959 Points
193 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 02, 2012 12:58 PM|LINK
I think to solve this issue you have to move all you form controls including the validation summary into a partial view and call that partial view in your view inside the form.
@using (Ajax.BeginForm("CreateAll", new AjaxOptions { UpdateTargetId = "YourControl" })) { <div id="YourControl"> @Html.RenderPartial("CreateAllPartial", Model); </div> }And you controller method can look like below
[HttpPost] [ValidateAntiForgeryToken] public ActionResult CreateAll(VisitLabResult vlr, int visitid ) { try { if (ModelState.IsValid) { vlr.VisitID = visitid; repository.AddVisitLabResult(vlr); repository.Save(); return view("CreateAllPArtial",vlr); } } catch (DbUpdateException) { JsonRequestBehavior.AllowGet); ModelState.AddModelError(string.Empty, "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests"); return view("CreateAllPArtial",vlr); }My code might look stupid, but my intention is to convey the idea!
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
johnjohn1231...
Participant
922 Points
871 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 02, 2012 01:21 PM|LINK
but in this way i will only be showing one Ajax.BeginForm instead of 10...
gopakumar.r
Participant
959 Points
193 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 02, 2012 01:27 PM|LINK
You will have to create partial views for each form
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
bruce (sqlwo...
All-Star
36882 Points
5451 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 02, 2012 01:30 PM|LINK
you are using an ajax form that replaces the html of a node with the server response. if there is an error, then you need to have the error message in that response, currently you have none.
you coudl turn your form into a partial view, that is used on the main page, and returned by the post action.
johnjohn1231...
Participant
922 Points
871 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 02, 2012 03:40 PM|LINK
thanks a lot for your help, i have updated my view and action method as shown below. Now i am returning a partial view in case of success or failure,, things are working fine except that when i am returning a partial view from my action method the partial view table columns will be displayed below each other instead of being displayed beside each other(since they are table columns at the same table row) so they look out of content,, so how i can fix this user interface issue.
My view looks as
@for (int item = 0; item < 10; item++) { using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = item.ToString(), InsertionMode = InsertionMode.Replace, OnSuccess = string.Format( "disableform({0})", Json.Encode(item)), })) { <tr id = "@item"> @Html.Partial("_create",Model) </tr> } } </table>My partial view looks as
@section scripts{ <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/Medicalscript.js")" type="text/javascript"></script> } <td> @Html.DropDownList("LabTestID", String.Empty) @Html.ValidationMessageFor(model => model.LabTestID) </td> <td> @Html.EditorFor(model => model.Result) @Html.ValidationMessageFor(model => model.Result) </td> <td> @Html.EditorFor(model => model.DateTaken) @Html.ValidationMessageFor(model => model.DateTaken) </td> <td> @Html.EditorFor(model => model.Comment) @Html.ValidationMessageFor(model => model.Comment) </td> <td> <input type= "hidden" name = "visitid" value = "@ViewBag.visitid" /> <input type="submit" value="Create" /> </td>My action method looks as:-
[HttpPost] [ValidateAntiForgeryToken] public ActionResult CreateAll(VisitLabResult vlr, int visitid ) { try { if (ModelState.IsValid) { var v = repository.GetVisit(visitid); if (!(v.EligableToStart(User.Identity.Name))) { return View("NotFound"); } vlr.VisitID = visitid; repository.AddVisitLabResult(vlr); repository.Save(); ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID); // return Json(new { IsSuccess = "True" }, JsonRequestBehavior.AllowGet); return PartialView("_create",vlr); } } catch (DbUpdateException) { // return Json(new { IsSuccess = "False", description = "" }, JsonRequestBehavior.AllowGet); ViewBag.LabTestID = new SelectList(repository.FindAllLabTest(), "LabTestID", "Description", vlr.LabTestID); ModelState.AddModelError(string.Empty, "The Same test Type might have been already created,, go back to the Visit page to see the avilalbe Lab Tests"); return PartialView("_create", vlr);I tried the above on firefox and chrome and the table column were displyed correctly,, but on IE the problem still there
RichardY
Star
8376 Points
1573 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 02, 2012 03:58 PM|LINK
I don't see an opeing <table> tag in your view. Also, check that the <tr> tags are remaing in the rendered output. I think the replace ajax option might replace the entire element including the <tr>. If so just put <tr> tags in your partial.
johnjohn1231...
Participant
922 Points
871 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 02, 2012 04:06 PM|LINK
thanks for the reply ,i tried to put <tr> in the partial view but it did not work also, beside this i have an opening <table> tag , but i have only provided part of my view code to ease understanding my problem. here is my full view code:-
@model Medical.Models.VisitLabResult @{ ViewBag.Title = "Create"; } <h2>Create @ViewBag.visitid</h2> <table> <tr> <th> Lab Test </th> <th> Result </th> <th> Date Taken </th> <th> Comment </th> <th> </th> <th> </th> </tr> @for (int item = 0; item < 10; item++) { using (Ajax.BeginForm("CreateAll", "VisitLabResult", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = item.ToString(), InsertionMode = InsertionMode.Replace, OnSuccess = string.Format( "disableform({0})", Json.Encode(item)), })) { @* <td id = "@(item.ToString() + "td")" style="color: #7fb067; font-weight: bold"> </td>*@ <tr id = "@item"> @Html.Partial("_create",Model) </tr> } } </table> <div> @Html.ActionLink("Back To Current Visit", "Edit", "Visit", new { id = ViewBag.visitid }, null) </div>gopakumar.r
Participant
959 Points
193 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 03, 2012 05:09 AM|LINK
I think the problem is the way sections are rendered when they are put in partial views. I think your script sections in the partial view is not being put into the rendersection block in your layout/view. Try moving all your script into another partial view and render that partial view where ever you want it to be rendered.
The sections doesnt work if your rendersection is defined in your layout, as the partial view doesnt have layout defined, only your view has the layout defined. So section defined in your view will get rendered in the rendersection block. Dont ask me why !, i have seen this behaviour in razor.
Hope i am hitting the right target!
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |
gopakumar.r
Participant
959 Points
193 Posts
Re: ModelState.AddModelError is not being displayed inside my view
May 03, 2012 05:28 AM|LINK
Found a link which explains how nested sections work and how to get it work, might be helpful
http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx
Gopakumar
| Please click “Mark as Answer” on the post(s) if it helps |