This problem is very strange, maybe I have missed some small detail, since I’m new to mvc 3…
In a partial view, I have a different model than in the main view (I don’t think that it matters in this case). I have added a couple of Validation-helper calls in the view. No matter what I do, they dont show up on the page.
I have enabled framework source debugging, and I can see that the HTML is generated, and are written to “output”. Still, they dont appear in the final rendered page. I cannot understand why.
In my partial view:
@model ModelsDTO.Domain.GridRowDTO
@Html.ValidationSummary()
@Html.TextBox("Kalla")
@Html.ValidationMessage("Kalla")
I have the textbox there just to see if it renders. It does.
My controller code (hard coded message, just to try to make it work):
[HttpPost]
public ActionResult SaveGridRow(GridRowDTO rad)
{
List<string> valideringsFel = _dagboksanteckning.SaveDagbokObjekt(rad);
ModelState.AddModelError("Kalla", "Källan felaktig");
return PartialView("_DagbokGrid", rad);
}
The model:
public class GridRowDTO
{
public string Kronika { get; set; }
public string Ok { get; set; }
public string Datum { get; set; }
public string Tid { get; set; }
public string Kalla { get; set; }
public string Handelse { get; set; }
public string Bedomning { get; set; }
public string Till { get; set; }
public string Rubrik { get; set; }
public string Verksamhetsslag { get; set; }
public string OperationPadrag { get; set; }
public string Verksamhetskod { get; set; }
public string LatitudTecken { get; set; }
public string Latitud { get; set; }
public string LongitudTecken { get; set; }
public string Longitud { get; set; }
public string Media { get; set; }
public string AnnatDokument { get; set; }
public string Region { get; set; }
public string id { get; set; }
}
Edit, very interesting finding!
When tracing the call with IE9 F12-mode, the response text acutally contains the expected HTML! Why does’nt it render!
<div class="validation-summary-errors"><ul><li>Källan felaktig</li>
</ul></div>
<input class="input-validation-error" id="Kalla" name="Kalla" type="text" value="1" />
<span class="field-validation-error">Källan felaktig</span>
I would be really thankfull if I could get some assistance to understand this problem.
If you are calling this controller action using AJAX you should make sure that you are substituting the contents of the partial with the new value. For example assuming your partial is wrapped inside a div:
Now inside the success callback make sure you have refreshed the contents of the div:
or if you are using Ajax.* helpers to call the action make sure you have specified
UpdateTargetIdin the AjaxOptions and that this value corresponds to the id of some DOM element you want to refresh.