請耐心等待我的noobness,我是超新的MVC模式。MVC 4 - 在局部視圖中使用不同的模型
我試圖做
我建立給註冊用戶的個人資料信息頁上我的網站。這個頁面將列出關於用戶的數據,例如出生日期,電話號碼,訂閱狀態等等。你明白了。我還想要一個表單讓用戶在同一頁上更改他們的密碼,電子郵件地址,個人信息。
我的問題
通過傳遞模型變量用戶的數據來自我的控制器:
public ActionResult Profil()
{
var model = db.Users.First(e => e.UserName == WebSecurity.CurrentUserName);
return View(model);
}
輸出看起來像這樣在我的觀點:
<label>Phone number: </label>
@if (Model.PhoneNumber != null)
{
@Model.PhoneNumber
}
else
{
<span class="red">You haven't set up your phone number yet. </span>
}
形式其中用戶可以更改他的信息將使用另一個模型ProfileModel。所以basiccaly我需要在我看來使用兩個模型,一個用於輸出信息,另一個用於發佈數據。我認爲,使用局部視圖,我可以做到這一點,但我得到這個錯誤:
The model item passed into the dictionary is of type 'Applicense.Models.User', but this dictionary requires a model item of type 'Applicense.Models.ProfileModel'.
這裏是我的局部視圖調用如下:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
@Html.Partial("_ModifyProfileInfo")
}
這裏的局部視圖:
@model Applicense.Models.ProfileModel
<ul>
<li>
@Html.LabelFor(m => m.Email)
@Html.EditorFor(m => m.Email)
</li>
<li>
@Html.LabelFor(m => m.ConfirmEmail)
@Html.EditorFor(m => m.ConfirmEmail)
</li>
<input type="submit" value="Update e-mail" />
</ul>
最後這裏是我的ProfileModel:
public class ProfileModel
{
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "New e-mail address")]
public string Email { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Confirm new e-mail address")]
[Compare("Email", ErrorMessage = "The e-mail and it's confirmation field do not match.")]
public string ConfirmEmail { get; set; }
}
我錯過了什麼嗎?什麼是正確的方法來做到這一點?
編輯: 我重拍我的代碼反映了尼古拉Mitev的答案,但現在我有另一個問題。這裏是我得到的錯誤:
Object reference not set to an instance of an object. (@Model.UserObject.LastName)
這隻發生在我發佈更改的電子郵件地址值時。這是我的視圖模型(ProfileModel.cs):
public class ProfileModel
{
public User UserObject { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Új e-mail cím")]
public string Email { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name = "Új e-mail cím megerősítése")]
[Compare("Email", ErrorMessage = "A két e-mail cím nem egyezik.")]
public string ConfirmEmail { get; set; }
[DataType(DataType.EmailAddress)]
[Display(Name= "E-mail cím")]
public string ReferEmail { get; set; }
}
控制器:
public ActionResult Profil()
{
var User = db.Users.First(e => e.UserName == WebSecurity.CurrentUserName);
var ProfileViewModel = new ProfileModel
{
UserObject = User
};
return View(ProfileViewModel);
}
最後,這裏是我user.cs
模型類:
[Table("UserProfile")]
public class User
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Column("UserName")]
public string UserName { get; set; }
[Column("Email")]
[Required]
public string Email { get; set; }
[Column("FirstName")]
public string FirstName { get; set; }
[Column("LastName")]
public string LastName { get; set; }
[Column("PhoneNumber")]
public string PhoneNumber { get; set; }
... You get the idea of the rest...
我想它的發生,因爲該模型是試圖將每個required
列中的數據放入數據庫中。
EDIT2: 我PROFIL行動的httppost方法:
[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Profil(ProfileModel model)
{
if (ModelState.IsValid)
{
//insert into database
return Content("everything's good");
}
else
{
//outputs form errors
return View(model);
}
}
謝謝你,就像一個魅力! :) +1也寫得很好的答案。 – PeterInvincible
現在我得到另一個錯誤:(我編輯的問題,以反映它 – PeterInvincible
@Tharline做你的特定記錄,你試圖獲取在LastName字段中的值? –