0

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&#228;llan felaktig</li>
</ul></div>

<input class="input-validation-error" id="Kalla" name="Kalla" type="text" value="1" />
<span class="field-validation-error">K&#228;llan felaktig</span>

I would be really thankfull if I could get some assistance to understand this problem.

3
  • pls paste the code for ur model.."rad" Commented Apr 20, 2012 at 6:09
  • is that request is AJAX or HTTP request Commented Apr 20, 2012 at 6:10
  • It's..... AJAX i think (what jqGrid does when saving a grid row). Commented Apr 20, 2012 at 6:25

1 Answer 1

2

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:

<div id="container">
    @Html.Patrial(....)
</div>

Now inside the success callback make sure you have refreshed the contents of the div:

$.ajax({
    url: '@Url.Action("SaveGridRow")',
    type: 'POST',
    data: ...,
    success: function(result) {
        // Here you must refresh the div or whatever part of the DOM
        // you need to update
        $('#container').html(result);
    }
});

or if you are using Ajax.* helpers to call the action make sure you have specified UpdateTargetId in the AjaxOptions and that this value corresponds to the id of some DOM element you want to refresh.

Sign up to request clarification or add additional context in comments.

1 Comment

Hell yeah! Thank you for this! I know enough MVC.NET to be dangerous (but not nearly enough jQuery to be dangerous) and I was running into an issue where my ValidationMessageFor helpers weren't displaying data in my partial. I read a bunch of posts about unobtrusive this'n'that but the thing that was different for me is that I didn't have embedded Html.BeginForms or Ajax.BeginForms so I didn't know what the deuce was going on until I read your post. Thanks again! +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.