创建包括在MVC3其他车型的对象视图模型 [英] Creating View Models consisting of objects of other models in MVC3

查看:142
本文介绍了创建包括在MVC3其他车型的对象视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,该模型,我将连成一个视图模型在asp.net MVC3像这样的枚举。

I have a model and an enumerable of that model that I am putting together into a view model in asp.net mvc3 like so.

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>();
    }
}

现在我强烈地键入一个视图使用视图模型。

Now I strongly type a view to use the view model.

@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>

有关职位某种原因,它不填充视图模型中的对象。所以,当我创建了一个动作后(UserMaintenanceViewModel模型)的所有值都为空。像这样..

For some reason on post it does not populate the object within the view model. So when i create a post action all values of (UserMaintenanceViewModel model) are null. Like so..

 [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,而不是预期的用户名。任何指导,将大大AP preciated我觉得我应该能够做到这一点。

Also, if I switch to getting postback through a (FormCollection form) I see that the names of the html elements are "user.UserName" instead of the expected "UserName". Any guidance would be greatly appreciated I feel like I should be able to accomplish this.

非常感谢

推荐答案

如果你有一个复杂的视图模型这是你所拥有的,那么你必须确保在构造函数来创建对象的实例。然后,您可以添加信息,以任何子对象,因为它现在有一个实例。

If you have a complex view model which is what you have, then you have to make sure to create instance of an object in the constructor. You can then add info to any child object since it now has an instance.

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; }

}

}

这篇关于创建包括在MVC3其他车型的对象视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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