asp.net-mvc - 如果(ModelState.IsValid==false)返回View();或 View (模型);?

标签 asp.net-mvc asp.net-mvc-2

当验证失败时,我应该返回哪一个?看法();或 View (模型); ?

我注意到两者都有效。这很令人困惑。

编辑:

public class MoviesController : Controller
{
    MoviesEntities db = new MoviesEntities();

    //
    // GET: /Movies/

    public ActionResult Index()
    {
        var movies = from m in db.Movies
                     select m;
        return View(movies.ToList());
    }

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

    [HttpPost]
    public ActionResult Create(Movie movie)
    {
        if (ModelState.IsValid)
        {
            db.AddToMovies(movie);
            db.SaveChanges();

            return RedirectToAction("Index");
        }
        else
            return View();//View(movie);
    }
}

我的Create.aspx:

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>

    <fieldset>
        <legend>Fields</legend>


        <div class="editor-label">
            <%: Html.LabelFor(model => model.Title) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Title) %>
            <%: Html.ValidationMessageFor(model => model.Title) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.ReleaseDate) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.ReleaseDate) %>
            <%: Html.ValidationMessageFor(model => model.ReleaseDate) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Genre) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Genre) %>
            <%: Html.ValidationMessageFor(model => model.Genre) %>
        </div>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Price) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Price) %>
            <%: Html.ValidationMessageFor(model => model.Price) %>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

最佳答案

如果您返回的 View 是强类型的并使用该模型,那么最好传递该模型。如果您只是返回 View() 并在 View 中尝试访问模型,您很可能会收到 NullReferenceException

以下是常见模式:

public class HomeController: Controller
{
    public ActionResult Index()
    {
        var model = FetchModelFromRepo();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(SomeViewModel model)
    {
        if (!ModelState.IsValid)
        {
            return View(model);
        }        

        // TODO: update db
        return RedirectToAction("index");
    }
}

关于asp.net-mvc - 如果(ModelState.IsValid==false)返回View();或 View (模型);?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3803257/

相关文章:

c# - WCF 数据服务 SaveChanges() : "Resource not found for the segment"

Javascript dialog() 函数在我的 MVC 代码中不起作用

html - Kendo UI Sprite.png 显示在桌面但不显示在选项卡中(所有浏览器)

java - MVC4 验证在 IIS 部署后不起作用,但在 Visual Studio 服务器中工作正常

C# Automapper 在 Null 时忽略属性

asp.net-mvc - MVC5 中的自定义验证错误消息

jquery - 使用 AJAX 在 Controller 中接收空模型

wcf - 如何将 T (IEnumerable<T>) 数组转换为 IQueryable<T> 以便使用 LINQ 进行处理?

.net - ASP.NET MVC 2 中是否可以嵌套区域?

asp.net-mvc-3 - MVC 2/MVC 3/MVC 4中的嵌套区域