사전을 바인딩해야 각 값에 편집 할 텍사스 상자가 있어야하므로 아래의 방법으로 작동하게 할 수 있습니다. HTML에서 이름 속성이 생성되는 방식에 영향을주는 정말로 중요한 부분은 모델 표현식입니다. 이는 모델 바인딩이 다시 게시에서 발생하는 것을 보장합니다. 이 예제는 Dictionary에서만 작동합니다.
링크 된 문서 바인딩 작업을하게 HTML 구문을 설명하지만이 꽤 신비를 달성 할 수있는 면도기 구문을 떠난다. 또한 키와 값을 모두 편집 할 수 있으며 사전의 키가 정수가 아닌 정수 인 경우에도 정수 인덱스를 사용한다는 점에서이 기사는 상당히 다릅니다. 그러므로 사전을 묶으려는 경우, 어떤 시나리오를 취할 것인지 결정하기 전에 먼저 값을 편집 할 수 있는지, 아니면 키와 값을 모두 원하는지 먼저 평가해야합니다. 왜냐하면 그러한 시나리오가 매우 다르기 때문입니다.
혹시 복잡한 개체를 바인딩해야하는 경우
, 즉 사전 당신은 단지 표현이 기사 유사한 재산에 드릴링과 각 속성에 대한 텍스트 상자를 가질 수 있어야합니다.
http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
public class SomeVM
{
public Dictionary<string, string> Fields { get; set; }
}
public class HomeController : Controller
{
[HttpGet]
public ViewResult Edit()
{
SomeVM vm = new SomeVM
{
Fields = new Dictionary<string, string>() {
{ "Name1", "Value1"},
{ "Name2", "Value2"}
}
};
return View(vm);
}
[HttpPost]
public ViewResult Edit(SomeVM vm) //Posted values in vm.Fields
{
return View();
}
}
CSHTML : 값에 대한
편집자는 (물론 당신이 동시에, labelFor은 키에 따라 라벨을 생성하기 위해 추가 할 수 있습니다) : 두 키를 편집
@model MvcApplication2.Controllers.SomeVM
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>SomeVM</legend>
@foreach(var kvpair in Model.Fields)
{
@Html.EditorFor(m => m.Fields[kvpair.Key]) //html: <input name="Fields[Name1]" …this is how the model binder knows during the post that this textbox value gets stuffed in a dictionary named “Fields”, either a parameter named Fields or a property of a parameter(in this example vm.Fields).
}
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
를/값 : @ {var fields = Model.Fields.ToList(); }
@for (int i = 0; i < fields.Count; ++i)
{
//It is important that the variable is named fields, to match the property name in the Post method's viewmodel.
@Html.TextBoxFor(m => fields[i].Key)
@Html.TextBoxFor(m => fields[i].Value)
//generates using integers, even though the dictionary doesn't use integer keys,
//it allows model binder to correlate the textbox for the key with the value textbox:
//<input name="fields[0].Key" ...
//<input name="fields[0].Value" ...
//You could even use javascript to allow user to add additional pairs on the fly, so long as the [0] index is incremented properly
}
"작동하지 않는다"는 것을 의미하십시오. –
정확히 작동하지 않는 것은 무엇입니까? 조금 더 자세히 설명해 주시겠습니까? – Jimmeh
보기에서 업데이트 된 데이터를 처리하려고하므로 컨트롤러에서 올바른 업데이트 된 모델을 받고 있지만이 특정 속성에는 값이 없습니다. 내 경우에는 [null, null]입니다. –