我是ASP.MVC4的新手,我面临着将对象从视图传递到控制器的问题: 让我从一开始就解释我的问题: 我使用的类是例如:UserAndRolesModel
public class UserAndRolesModel
{
public user User { get; set; }
public IEnumerable<UserAndRoles> AsignedRoles { get; set; }
}
正如您可以看到类UserAndRolesModel持久存在2个对象:user和Enumerable
public class user
{
[HiddenInput(DisplayValue=false)]
public virtual int user_id { get; set; }
[Required(ErrorMessage = "Please provide name")]
[Display(Name = "Name")]
public virtual string user_name { get; set; }
[Display(Name = "Is active?")]
public virtual bool user_active { get; set; }
}
public class UserAndRoles
{
public string RoleName { get; set; }
public bool IsAssigned { get; set; }
}
我的控制器操作很简单,它将用户分配给UserWithRoles.User并使用UserAndRoles对象创建tempList。
public ActionResult Edit(int userid=0)
{
//object which will be passed to View
UserAndRolesModel UserWithRoles = new UserAndRolesModel();
//user
UserWithRoles.User = repoUsers.Users.FirstOrDefault(x => x.user_id == userid);
//roles
IList<UserAndRoles> tempList = new List<UserAndRoles>();
UserAndRoles temp1 = new UserAndRoles();
temp1.RoleName="Admin";
temp1.IsAssigned=true;
UserAndRoles temp2 = new UserAndRoles();
temp2.RoleName="User";
temp2.IsAssigned=false;
tempList.Add(temp1);
tempList.Add(temp2);
//assign tempList to model
UserWithRoles.AsignedRoles = tempList;
return View(UserWithRoles);
)
在此阶段,我成功传递给View:
UserWithRoles.User.user_id=1;
UserWithRoles.User.user_name="UserName1";
UserWithRoles.User.user_active=true;
UserWithRoles.AsignedRoles[1].RoleName = "Admin";
UserWithRoles.AsignedRoles[1].IsAssigned = true ;
UserWithRoles.AsignedRoles[2].RoleName = "User";
UserWithRoles.AsignedRoles[2].IsAssigned = false;
我能够正确显示以上视图:
@model Models.UserAndRolesModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="editor-field">
@Html.EditorFor(model => model.User.user_id)
@Html.ValidationMessageFor(model => model.User.user_id)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User.user_name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.User.user_name)
@Html.ValidationMessageFor(model => model.User.user_name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.User.user_active)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.User.user_active)
@Html.ValidationMessageFor(model => model.User.user_active)
</div>
<br /> ROLES below is piece of code which makes me cry<br />
@foreach (var item in Model.AsignedRoles)
{
<div class="editor-field">
<div class="editor-field">
@Html.LabelFor(model => item.RoleName)
@Html.CheckBoxFor(model => item.IsAssigned)
</div>
</div>
}
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
当我点击提交时,有关用户的所有数据都已正确传递,但IEnumerable AsignedRoles始终为null。 这是我在帖子上的控制器方法:
[HttpPost]
public ActionResult Edit(UserAndRolesModel UserWithRoles)
{
if (ModelState.IsValid)
{
if (UserWithRoles.AsignedRoles==null)
Console.WriteLine("I am still crying");
else
Console.WriteLine("Got it!");
}
return View(UserWithRoles);
}
在视图中,我尝试使用其他循环,例如:
@for (int i = 0; i < Model.AsignedRoles.Count(); i++)
{
<div class="editor-field">
@Html.LabelFor(model => item[i].IsAssigned)
@Html.CheckBoxFor(model => item[i].IsAssigned)
</div>
}
但上面也没有通过IEnumerable。 任何人都可以帮我解决这个问题吗?我怎样才能传回包含IEnumerable的控制器UserAndRolesModel? 我将非常感激。感谢您的帮助!
答案 0 :(得分:1)
您确实需要for
循环,但您尝试过的循环已引用item[i]
,但item[i]
不再存在。试试这个,请注意我还为HiddenFor
添加了RoleName
,否则将无法传回:
@for (int i = 0; i < Model.AsignedRoles.Count(); i++)
{
<div class="editor-field">
@Html.LabelFor(model => model.AssignedRoles[i].IsAssigned)
@Html.CheckBoxFor(model => model.AssignedRoles[i].IsAssigned)
@Html.HiddenFor(model => model.AssignedRoles[i].RoleName)
</div>
}