asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 如何在验证集合中添加验证错误asp.net mvc?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_874_0@
在我的控制器的动作中,我有以下代码:
public ActionResult GridAction(String id)
{
    if (String.IsNullOrEmpty(id)) 
    {
        // add errors to the errors collection and then return the view saying that you cAnnot SELEct the dropdownlist value with the "Please SELEct" option
    }

    return View(); 
}

更新:

if (String.IsNullOrEmpty(id))
{
    // add error 
    ModelState.AddModelError("GridActionDropDownList","Please SELEct an option");
    return RedirectToAction("orders"); 
}

更新2:

这是我更新的代码:

@Html.DropDownListFor(x => x.SELEctedGridAction,Model.GridActions,"Please SELEct") 
@Html.ValidationmessageFor(x => x.SELEctedGridAction)

该模型如下所示:

public class MyInvoicesViewModel
{

    private List<SELEctListItem> _gridActions;

    public int CurrentGridAction { get; set; }

    [required(Errormessage = "Please SELEct an option")]
    public String SELEctedGridAction { get; set; }

    public List<SELEctListItem> GridActions
    {
        get
        {
            _gridActions = new List<SELEctListItem>();
            _gridActions.Add(new SELEctListItem() { Text = "Export to Excel",Value = "1" });

            return _gridActions;
        }
    }
}

这里是我的控制器动作:

public ActionResult GridAction(String id)
{
    if (String.IsNullOrEmpty(id))
    {
        // add error 
        ModelState.AddModelError("SELEctedGridAction","Please SELEct an option");
        return RedirectToAction("orders"); 
    }

    return View(); 
}

什么都没发生!我完全迷失在这一个!

更新3:

我现在使用以下代码,但仍然验证不会触发:

public ActionResult GridAction(String id)
{
    var myViewModel= new MyViewModel();
    myViewModel.SELEctedGridAction = id; // id is passed as null           

    if (!ModelState.IsValid)
    {
        return View("orders");
    }

更新4:

$("#linkGridAction").click(function () {
    alert('link grid action clicked'); 

    $.get('GridAction/',{ SELEctedGridAction: $("#SELEctedGridAction").val() },function (result) {
        alert('success');
    });
});

控制器如下所示:

// OrderViewModel has a property called SELEctedGridAction. 
public ActionResult GridAction(OrderViewModel orderViewModel)
{
    return View(); 
}

更新5:验证没有触发:

public ActionResult GridAction(OrderViewModel orderViewModel)
{
    if (!ModelState.IsValid)
    {
        return View("orders",orderViewModel); 
    }
    return View(); 
}

解决方法

您可以使用视图模型:
public class MyViewModel
{
    [required]
    public String Id { get; set; }
}

接着:

public ActionResult GridAction(MyViewModel model)
{
    if (ModelState.IsValid)
    {
        // the model is valid,the user has SELEcted an id => use it
        return RedirectToAction("success");
    }
    return View();
}

更新:

在对我的答复的数百条评论之后,我觉得有必要提供一个充分的工作实例:

像往常一样,从视图模型开始:

public class MyViewModel
{
    [required]
    public String SELEctedItemId { get; set; }

    public IEnumerable<SELEctListItem> Items 
    {
        get
        {
            // Dummy data
            return new SELEctList(Enumerable.Range(1,10)
                .SELEct(i => new SELEctListItem 
                {
                    Value = i.ToString(),Text = "item " + i 
                }),"Value","Text");
        }
    }
}

然后一个控制器:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [httpPost]
    public ActionResult Index(MyViewModel model)
    {
        if (!ModelState.IsValid)
        {
            // The user didn't SELEct any value => @R_772_9066@play the form
            return View(model);
        }
        // TODO: do something with model.SELEctedItemId
        return RedirectToAction("success");
    }
}

最后的看法:

<% using (Html.beginForm()) { %>
    <%= Html.DropDownListFor(
        x => x.SELEctedItemId,Model.Items,"-- SELEct Item --"
    ) %>
    <%= Html.ValidationmessageFor(x => x.SELEctedItemId) %>
    <input type="submit" value="OK" />
<% } %>

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc – 如何在验证集合中添加验证错误asp.net mvc?全部内容,希望文章能够帮你解决asp.net-mvc – 如何在验证集合中添加验证错误asp.net mvc?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。