にビューからメンバーオブジェクトまたはメンバーのリストを持つオブジェクトを渡しますここに私の図である。ここではは、コントローラ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate.AspNet.Identity;
namespace MyProject.Web.Models
{
public class IdentityRoleView
{
public virtual string Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<IdentityUser> Users { get; set; }
}
}
は私のコントローラである:ここでは
[HttpGet]
public ActionResult Edit(string roleId)
{
IdentityRole role = Service.Find(roleId);
return View("Edit", AutoMapper.Mapper.Map<IdentityRoleView>(role));
}
[HttpPost]
public ActionResult Edit(IdentityRoleView role)
{
Service.Update(role);
TempData["Comment"] = "The record was updated";
return RedirectToAction("Index");
}
}
が私の見解です。
@model MyProject.Web.Models.IdentityRoleView
@{
ViewBag.Title = "Edit";
Layout = "~/Areas/Administration/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.Id);
<div>
Role name
</div>
<p>
@Html.TextBoxFor(model => model.Name)
</p>
for (int items = 0; items < Model.Users.Count; items++)
{
@Html.DisplayFor(m => m.Users[items].Id);
@Html.DisplayFor(m => m.Users[items].UserName);
}
<input type="submit" value="Save" />
}
IdentityRoleView.Usersは、コントローラに戻すと常にnullです。何が問題ですか? IdentityRoleView.Usersがビューに表示されています。IdentityRoleView.Usersが常にnullであるため、サーバーにポストバックされるとすべて削除されます。ここで
あなたのフォームにそのリストのフォーム値が存在しないので、少なくとも今は投稿していません。あなたが持っている唯一のフォーム値は、 "名前"プロパティです。モデルのリストにモデルバインダーを使用する方法については、私はそれのための既製のコードはありませんが、これは始めるのに最適な場所のようです:https://stackoverflow.com/questions/19964553/mvc-オブジェクトのリストの後にフォームできない – David
これで、現在の値をコントローラのPOSTメソッドに戻したいだけですか?それですか? 明確にしようとしています。 –