asp.net-mvc-3 - 在 MVC3 中创建由其他模型的对象组成的 View 模型

标签 asp.net-mvc-3 c#-4.0 viewmodel

我有一个模型和该模型的枚举,我将它们一起放入 asp.net mvc3 中的 View 模型中,如下所示。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;


namespace FuelPortal.Models.DBModels
{
    public class UserDBModel
    {

        public int userId { get; set; }

        [Required, Display(Name = "User Name")]
        public string userName { get; set; }

        [Required, Display(Name = "Password")]        
        public string password { get; set; }

        [Required, Display(Name = "First Name")]
        public string firstName { get; set; }

        [Required, Display(Name = "Last Name")]
        public string lastName { get; set; }

        [Required, Display(Name = "Email")]
        public string email { get; set; }

        [Display(Name = "Phone")]
        public string phone { get; set; }

        [Display(Name = "Account Expiration Date(Optional)")]
        public DateTime expireDate { get; set; }

        [Display(Name = "Password Expiration Date")]
        public DateTime passwordExpireDate { get; set; }

        [Display(Name = "Last Date Modified")]
        public DateTime DateModified { get; set; }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FuelPortal.Models.DBModels;

namespace FuelPortal.Models.ViewModels
{
    public class UserMaintenanceViewModel
    {

        public UserDBModel user = new UserDBModel();
        public List<UserDBModel> userList = new List<UserDBModel>();
    }
}

现在我强类型一个 View 来使用 View 模型。

@model FuelPortal.Models.ViewModels.UserMaintenanceViewModel  
<div class="frmBG">
        @using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "wrapperData" }))
        {

                <h5>Create New User</h5> 
                <br />
                <table cellpadding="2" cellspacing="10" border="0">
                <tr><td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.user.userName)
                </div></td><td>

                <div class="editor-field">
                    @Html.EditorFor(model => model.user.userName)
                    @Html.ValidationMessageFor(model => model.user.userName)
                </div></td></tr>

                <tr><td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.user.password)
                </div></td><td>
                <div class="editor-field">
                    @Html.PasswordFor(model => model.user.password)
                    @Html.ValidationMessageFor(model => model.user.password)
                </div></td></tr>

                <tr><td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.user.firstName)
                </div></td><td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.user.firstName)
                    @Html.ValidationMessageFor(model => model.user.firstName)
                </div></td></tr>

                <tr><td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.user.lastName)
                </div></td><td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.user.lastName)
                    @Html.ValidationMessageFor(model => model.user.lastName)
                </div></td></tr>

                <tr><td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.user.email)
                </div></td><td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.user.email)
                    @Html.ValidationMessageFor(model => model.user.email)
                </div></td></tr>

                <tr><td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.user.phone)
                </div></td><td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.user.phone)
                    @Html.ValidationMessageFor(model => model.user.phone)
                </div></td></tr>

                <tr><td>
                <div class="editor-label">
                    @Html.LabelFor(model => model.user.expireDate)
                </div></td><td>
                <div class="editor-field">
                    @Html.EditorFor(model => model.user.expireDate)
                    @Html.ValidationMessageFor(model => model.user.expireDate)
                </div></td></tr>

                <tr><td></td><td>
                <p>
                    <input type="submit" value="" class="create" /><input type="button" value="" class="close_tab"/>
                </p></td></tr>
                </table>
        }
    </div>

出于某种原因,在发布时它不会填充 View 模型中的对象。因此,当我创建后操作时,(UserMaintenanceViewModel 模型)的所有值均为 null。就像这样..

 [HttpPost]
        public ActionResult CreateUser(UserMaintenanceViewModel model)
        {
            try
            {
                /// TODO: Add insert logic here
                var result = repository.insertNewUser(Session["UserId"].ToString(), model.user.UserName, model.user.Password, model.user.FirstName,
                                                      model.user.LastName, model.user.Email, model.user.Phone, model.user.ExpireDate);
                // If result > 0 user insert successful

此外,如果我切换到通过(FormCollection 表单)进行回发,我会看到 html 元素的名称是“user.UserName”,而不是预期的“UserName”。任何指导将不胜感激,我觉得我应该能够完成这个任务。

非常感谢

最佳答案

如果您有一个复杂的 View 模型,那么您必须确保在构造函数中创建对象的实例。然后,您可以将信息添加到任何子对象,因为它现在有一个实例。

public class MyViewModel
{


    public MyViewModel()
    {
        Customer= new CustomerViewModel ();
        Address = new AddressViewModel();
    }

    public int OrderNumber{ get; set; }
    public CustomerViewModel Customer  { get; set; }
    public AddressViewModel Address { get; set; }

}

}

关于asp.net-mvc-3 - 在 MVC3 中创建由其他模型的对象组成的 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713157/

相关文章:

android - 在android中的服务中访问viewModel

wpf - 当通过DataTemplate应用时,为什么没有代码隐藏我的WPF View 不起作用?

jquery - $.getJSON 不调用 "successful"函数

asp.net-mvc - Json ModelBinding 在 MVC3 RC2 中无法通过 Ajax 工作

c# - 使用远程验证的多字段验证

c#-4.0 - C# 动态对象的模拟/ stub 框架

c# - 发送电子邮件通知的设计问题

c - 是否可以从 C#.Net 调用 C 函数

c# - 安全等于 : Why is inequality still consistently less time?

c# - 2 个模型选择字符串