创建视图发布空对象 [英] Create view is posting null objects

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

问题描述

应该是一个很容易回答的问题。
我想创建一个视图对象。包含该对象的类由User类和密码。
当我点击提交按钮,控制器拿起密码和用户空值。
见Container类,控制器和视图下方;

Should be an easy question to answer. I am trying to create an object in a view. The class that contains the object consists of a User class and a password. When I click on the submit button, the Controller picks up null values for Password and User. See below the Container class, the Controller and the View;

public class UserExtended
{
    public UserITOC User { get; set; }
    public string Password { get; set; }
}

    [Authorize]
    public ActionResult Create()
    {
        return View(new UserExtended());
    }

    //
    // POST: /Dinners/Create

    [Authorize(Roles = "Administrator")]
    [HttpPost]
    public ActionResult Create(UserExtended user)
    {
        if (ModelState.IsValid)
        {
            // Create user in the User datatable
            SqlUsersRepository sqlRepository = new SqlUsersRepository();
            ITOCEntities db = new ITOCEntities();
            db.UserITOCs.AddObject(user.User);

            // Create user as an authenticated user within the Reader role.
            int i = user.User.EmailAddress.IndexOf('@') - 1;
            string userName = user.User.EmailAddress.Substring(0, i);
            string email = user.User.EmailAddress;
            Membership.CreateUser(userName, user.Password, email);
            Roles.AddUserToRole(userName, "Reader");                // Automatically assigned as a Reader
        }
        return View(new UserExtended());
    }

<%@页标题=LANGUAGE =C#的MasterPageFile =〜/查看/共享/的Site.Master
    继承=System.Web.Mvc.ViewPage%>

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>


    创建

Create

<h2>Create</h2>

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

    <fieldset>
        <legend>Fields</legend>

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

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

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

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

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

<% } %>

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

推荐答案

非常简单的解决办法:

public ActionResult Create(UserExtended user)

public ActionResult Create(UserExtended UserExtended)

这样的ModelBinder的将知道如何从请求重组的对象。

That way the ModelBinder will know how to reassemble the object from Request.

希望这有助于!

这篇关于创建视图发布空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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