Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have partial view where in the user edit the data and save the result.
@model CollectionGroupViewModel

@using (Html.BeginForm("EditCollectionGroup", "CollectionGroup", FormMethod.Post, new { @id = "EditCollection" }))
{
<table id="Table1" class="Table_sty">
        <tr>
            <td class="tblTransSearch_td">
                @Html.Label("Market*")
            </td>
            <td class="Wid200">
                @Html.TextBoxFor(m => m.ISOCountryCode, new { @class = "ReadOnly", @style = "width:70px;border-radius: 5px;", @readonly = "true" })
                @Html.HiddenFor(m => m.Id)
            </td>
        </tr>
        <tr>
            <td class="Wid200">
                @Html.Label("Collection Group Name*")
            </td>
            <td>
                @Html.TextBoxFor(m => m.Name, new { @class = "ReadOnly", @readonly = "true" })
            </td>
        </tr>
        <tr>
            <td class="Wid200">
                @Html.Label("Fulfil Via GTS*")
            </td>
            <td>
                @Html.TextBoxFor(m => m.ISOCountryCode, new { @class = "ReadOnly", @style = "width:70px;" })
            </td>
        </tr>
        <tr>
            <td class="Wid200">
                @Html.Label("Processing Centre*", new { @id = "lblProcessingCenter" })
            </td>
            <td class="Wid200">
                @Html.DropDownListFor(m => m.ProcessingCenterViewModel.ProcessingCenterName, new SelectList(ViewBag.ProcessingCenter, "Value", "Text"), "Please Select", new { @id = "selectCollectionMarket", @class = "processing_center_field clr_black" })

                @Html.ValidationMessageFor(m => m.ProcessingCenterViewModel.ProcessingCenterName)
            </td>
        </tr>
    </table>

<div id="Btns">
        <button id="btn_SaveChanges" type="submit" class="btn_SaveChanges_sty">Save</button>
        <button id="btn_Cancel" type="button" class="btn_cancel_sty">Cancel</button>
    </div>
}

Controller:
[HttpPost]
        public ActionResult EditCollectionGroup(string marketId, int? page, CollectionGroupViewModel collectionGroupDetails)
        {
            ErrorResponse errorResponse;
            CollectionGroupEntity serviceObject;

            if (ModelState.IsValid)
            {
                serviceObject = CollectionGroupMapper.ConvertModelToEntity(collectionGroupDetails);
                collectionGroupHelper.SaveCollectionGroup(serviceObject, out errorResponse);

                if (errorResponse == null)
                {
                    TempData["SuccessMsg"] = true;
                    return RedirectToAction("ManageCollectionGroup", new { marketId = marketId, filter = false, page = page });
                }
            }
            else
            {
                return Json(new
                {
                    result = false,
                    errors = string.Join(",",
                    ModelState.Values.Where(e => e.Errors.Count > 0)
                                     .SelectMany(e => e.Errors)
                                     .Select(e => e.ErrorMessage)
                                     .ToArray()
                                     )
                });
            }
}


My problem is, on server side validation I'm returning the Json object which holds the errors. How can raise the data annotation message based on this Json object.

Or is there any other way to do the same, do I need to make ajax call rathr than BeginForm()?

Thanks in advance
Posted

1 solution

Is there a particular reason why you are returning a json object for the errors and not using the ModelState.AddModelError?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900