我有一個模型和該模型的一個枚舉,我把它放在一起在asp.net mvc3中的視圖模型就像這樣。創建視圖MVC3中的其他模型的對象組成的模型
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>();
}
}
現在我強烈地鍵入一個視圖來使用視圖模型。
@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模型)的所有值都爲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」而不是預期的「用戶名」。任何指導將不勝感激我覺得我應該能夠做到這一點。
非常感謝
你嘗試增加對UserDBModel參數? –