Frage

Client side Validation and ValidationSummary is working fine for my project (MVC4 + Razor + Unobtrusive JS) but the Server side errors are not shown on my view and if there was any client side error, it does not get removed from the view (it does from the ModelState). I have tried on both Chrome14 and IE9

Server side errors are being added to the model as ModelState.AddModelError(string.Empty, ModelState.AllErrors()); and shown to the view as @Html.ValidationSummary(false).

Edit

Simple form submit is working fine, it shows multiple error messages returned from the server and updates error messages, but, ajax based form submit is not working error messages returned by ajax based form submit are not shown at all.

Here is a sample demonstration of how the request is being made

    @*... View contents related to Master Model  ...*@
    @using (Ajax.BeginForm("ActionToAddRecord", new AjaxOptions()))
    {
        @Html.Action("ActionToAddRecord")
        <input type="submit" value="Add Record"/>
    }
    @*... View contents related to Master Model  ...*@

ActionToAddRecord is a partial view representing a model contained by a master model

Errors are returned as

[HttpGet]
public ActionResult ActionToAddRecord()
{
    return View();
}

[HttpPost]
public ActionResult ActionToAddRecord(childModel model)
{
    ModelState.AddModelError(string.Empty, "First error message");
    ModelState.AddModelError(string.Empty, "Second error message");
    return View(model);
}

Edit

I saw a similar functionality in templated MVC application by VS2010, the dialog based log in form. Error messages are returned as Json and then JS is being used to display them, IMO, seems like MS made Ajax based requests quite easy and concise (Ajax.BeginForm) but missing the error handling part. Right now i am not willing to use JS for this, there might be a better way to get this type of error handling dealt automatically.

War es hilfreich?

Lösung

Solved, with a small error.

Master View

@*Master View Contents*@
        @using (Ajax.BeginForm("AddPaymentCurrency", new AjaxOptions { UpdateTargetId = "paymentCurrency" }))
        {
            <div id="paymentCurrency"> 
                @{Html.RenderPartial("PaymentCurrency", Model.PaymentCurrencyNew);}
            </div>
        }

PaymentCurrency View

@*Model Editors*@
@Html.ValidationSummary(false)
<input type="submit" value="Add Payment Currency"/>

<div id="paymentCurrencyList" style="width:inherit; height:auto; overflow:auto;"> 
    @Html.Action("PaymentCurrencyList")
</div>

Controller

[HttpPost]
public ActionResult AddPaymentCurrency(PaymentCurrency model)
{
    if (!ModelState.IsValid)
    {
        ModelState.AddModelError(string.Empty, ModelState.AllErrors());
        return View("PaymentCurrency", model);
    }
    //Add login
    return View("PaymentCurrency", model);
}

public ActionResult PaymentCurrencyList()
{
    //var list = getList
    return View(list);
}

small error

ValidationSummary is shown with fields highlighted and asterisk shown against them while adding invalid payment currency. Once a valid currency is added then ValidationSummary and asterisk is no more shown on invalid payment currency, just fields are highlighted.

Please help me to get it fixed, i would prefer not to change the current structure, otherwise i will start getting big error

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top