验证部分视图 [英] Validating partial view

查看:66
本文介绍了验证部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我有局部视图,在用户编辑数据并保存结果。

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()
                                     )
                });
            }
}





我的问题是,在服务器端验证我正在返回持有的Json对象错误。如何根据这个Json对象引发数据注释消息。



或者还有其他方法可以做同样的事情,我是否需要使用ajax调用rathr而不是BeginForm ()?



提前谢谢



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

推荐答案

你有什么特别的原因要回json错误的对象而不使用ModelState.AddModelError?
Is there a particular reason why you are returning a json object for the errors and not using the ModelState.AddModelError?


这篇关于验证部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆