如果modestate无效,我们如何向客户显示错误消息 [英] How we can show the error message to customer if modestate isinvalid

查看:76
本文介绍了如果modestate无效,我们如何向客户显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Hi,

[AcceptVerbs(HttpVerbs.Post)]
[GridAction]
public ActionResult _InsertIdConfiguration(IdConfigurationViewModel idsView)
{
    IdConfigDTO _idConfig = new IdConfigDTO();
    RepositoryMethodInvoker _mi = new RepositoryMethodInvoker();
    var _context = Request.GetContextData();
    SiteDTO _site;

    try
    {
        if (ModelState.IsValid)
        {
            model = new MasterDataModel(_context);
            if (idsView.SelectedSiteIIds != null)
            {
                _idConfig.Sites = new List<sitedto>();
                foreach (var _iid in idsView.SelectedSiteIIds)
                {
                    _site = new SiteDTO();
                    _site.SiteIId = _iid;
                    _idConfig.Sites.Add(_site);
                }
            }
            _idConfig.Type = idsView.Type;
            _idConfig.Increment = idsView.Increment;
            _idConfig.MinimumLength = idsView.MinimumLength;
            _idConfig.Prefix = idsView.Prefix;
            _idConfig.Suffix = idsView.Suffix;
            _idConfig.IdsIId = idsView.IdsIId;
            Func<decimal> _method = new Func<decimal>(() => model.UpdateIdConfiguration(_idConfig));
            _mi.ExecuteFunction(_method);
        }
        else
        {

        }
    }

    catch (FaultException ex)
    {
        return AppHelper.GetGridErrorResult(ex);
    }
    return _SelectIdConfiguration();
}


这是在else块中尝试过的编码,如何显示错误消息给客户id modelstate无效..


请帮助解决此问题.


this is the coding am tried in else block how i can show error message to customer id modelstate is invalid..


Please help to solve this...

推荐答案

框架将使用DataAnnotation添加模型中定义的错误消息,但您可以添加自定义验证错误:
The framework will add the error messages defined in the model with DataAnnotation, but you can add custom validation errors:
ModelState.AddModelError("fieldname", "error message");


在视图中,您可以添加


In the view you can add

Html.ValidationMessage("fieldname")

或ValidationMessageFor字段.或在表单附近的某处使用ValidationSummary.
您还可以使用ViewBag在控制器和视图之间传递数据.

请注意,如果使用从数据库生成的模型,则需要使用部分类来添加DataAnnotation.

更新:
您忘记与我们分享重要的信息:此动作在ajax backtalk中被调用.看到此线程:

or ValidationMessageFor to the fields. Or use ValidationSummary somewhere near the form.
You can also use ViewBag to pass data between controller and view.

Be aware that if you use model generated from database, you need to use partial classes to add DataAnnotation.

Update:
You forget to share an important information with us: this action is called in an ajax backtalk. See this thread: http://www.telerik.com/community/forums/aspnet-mvc/grid/how-to-return-error-information-to-grid-in-ajax-editing-mode.aspx[^]


这篇关于如果modestate无效,我们如何向客户显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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