MVC休息和回归的看法 [英] MVC Rest and returning views

查看:130
本文介绍了MVC休息和回归的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现我的控制器上的宁静的约定,但我不知道如何处理从创建动作发送回新建视图失败模型验证。

 公共类myController的:控制器
{
    公众的ActionResult指数()
    {
        返回查看();
    }    公众的ActionResult新()
    {
        返回查看();
    }    [HttpPost]
    公众的ActionResult创建(为MyModel模型)
    {
        如果(!ModelState.IsValid)
        {
             //要返回查看新,但与现有的模型
        }        //处理我的模型
        返回RedirectToAction(「指数」);
    }
}


解决方案

简单:

  [HttpPost]
公众的ActionResult创建(为MyModel模型)
{
    如果(!ModelState.IsValid)
    {
        返回视图(新模式);
    }    //处理我的模型
    返回RedirectToAction(「指数」);
}

I'm trying to implement the restful convention on my controllers but am not sure how to handle failing model validation in sending it back to the 'New' view from the Create action.

public class MyController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult New()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(MyModel model)
    {
        if(!ModelState.IsValid)
        {
             // Want to return view "new" but with existing model
        }

        // Process my model
        return RedirectToAction("Index");
    }
}

解决方案

Simply:

[HttpPost]
public ActionResult Create(MyModel model)
{
    if(!ModelState.IsValid)
    {
        return View("New", model);
    }

    // Process my model
    return RedirectToAction("Index");
}

这篇关于MVC休息和回归的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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