-1
をsubmiting前に、私はこの方法/コントローラがあります。編集フォームデータASP.NET MVCコントローラへ
public ActionResult Create([Bind(Include = "Id,Name")] RoleViewModel roleVM)
を、私はロールと権限の間で多対多の関係を持っている、との役割権限を投稿する必要があります方法、私はバインディングに権限を追加する方法を変更:
public ActionResult Create([Bind(Include = "Id,Name,Privileges")] RoleViewModel roleVM)
役割権限がjstreeコントロールを使用して表示され、私はJavascript配列に権限を持っている:
var arr = $('#tvPrivileges').jstree(false).get_checked();
var filledArr = new Array();
arr.forEach(fillPrivileges)
{ }
function fillPrivileges(item, index) {
var privilege = {
Id: item,
Name: ""
}
filledArr.push(privilege);
}
役割と権限モデル:
public class RoleViewModel
{
public RoleViewModel()
{
this.Privileges = new List<PrivilegeViewModel>();
}
public int Id { get; set; }
public string Name { get; set; }
public List<PrivilegeViewModel> Privileges { get; set; }
}
public class PrivilegeViewModel
{
public int Id { get; set; }
public string Name { get;
}
HTML:
@using (Html.BeginForm("Create", "RolesController", FormMethod.Post, new { id = "RoleForm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, Resources.Captions.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Privileges, Resources.Captions.Privileges, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div id="tvPrivileges"></div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="@Resources.Captions.Create" class="btn btn-primary" />
@Html.ActionLink(Resources.Captions.Cancel, "Index", null, new { @class = "btn btn-white"})
</div>
</div>
</div>
}
どのようにしてフォームを送信する前にモデルに権限を追加することができますか?
他のパラメータ*を追加してもよろしいですか? 'RoleViewModel'オブジェクトのバインディングに' Privileges'を追加しただけです。 –
*フォームを送信する前に、そのモデルに権限を追加するには?*あなたが投稿したメソッドは '[HttpPost] 'これは、それらの特権が追加される唯一の方法は、あなたが実際にサブミットするときであることを意味します。あなたは' [HttpGet] 'でこれを行う方法を尋ねていますか? –
申し訳ありませんが、私はバインディングに特権を追加しました。権限を追加してPOSTを使用してそれらを送信します – Samirovic