2013-07-08 2 views
1

변수 세트가 ExtendedProperty 인 엔터티가 있습니다. 이들은 키와 값을 가지고 있습니다. 내 HTML 면도기보기에서ASP에서 변수 수가 알려지지 않은 게시 데이터 가져 오기 MVC

, 나는이 있습니다

@if (properties.Count > 0) 
{ 
    <fieldset> 
     <legend>Extended Properties</legend> 
      <table> 
      @foreach (var prop in properties) 
      { 
       <tr> 
        <td> 
         <label for="[email protected]">@prop.Name</label> 
        </td> 
        <td> 
         <input type="text" name="[email protected]" 
           value="@prop.Value"/> 
        </td> 
       </tr> 
      } 
      </table> 
     </fieldset> 
} 

사용자가이 채워 일단 내가 내 컨트롤러에이 데이터에 액세스 할 수 있습니까? 수동 html 대신 모델 바인딩을 사용할 수 있도록이 작업을 수행 할 수있는 방법이 있습니까?

EDIT = 아직 모델을 사용하고 있으며 @Html.EditFor(m => m.prop)과 같은 것을 사용하는 다른 것들이 양식에 있습니다. 그러나 이러한 변수 속성을 통합 할 수있는 방법을 찾지 못했습니다.

감사합니다.

+0

입력 이름을 지정하면 [0 ] .... Properties [n], 모델 바인더는이 모델을 Properties라는 모델의 IEnumerable로 변환합니다. – cadrell0

+0

어떻게 이것을 모델 클래스와 통합 할 수 있습니까? – elite5472

+1

@ elite5472 http://stackoverflow.com/questions/17450772/asp-net-mvc4-dynamic-form-generation/17451048#17451048에서 내 대답을 살펴보십시오. 나는 그것이 같은 문제라고 믿는다. –

답변

2

의이 Model 다음은이 가정하자 (뷰 모델, 내가 선호) :

public class ExtendedProperties 
{ 
    public string Name { get; set; } 
    public string Value { get; set; } 

} 

public class MyModel 
{ 
    public ExtendedProperties[] Properties { get; set; } 
    public string Name { get; set; } 
    public int Id { get; set; } 
} 

당신은 같은 마크 업을 사용하여보기에이 모델을 바인딩 할 수 있습니다 : 마지막으로

@using (Html.BeginForm("YourAction", "YourController", FormMethod.Post)) 
{ 
    <input type="text" name="Name" /> 
    <input type="number" name="Id" /> 

    <input type="text" name="Properties[0].Name" /> 
    <input type="text" name="Properties[0].Value" /> 
    ... 
    <input type="text" name="Properties[n].Name" /> 
    <input type="text" name="Properties[n].Value" /> 
} 

, 당신을 액션 :

[HttpPost] 
public ActionResult YourAction(MyModel model) 
{ 
    //simply retrieve model.Properties[0] 
    //... 
} 
+0

감사합니다. – elite5472

+0

@ elite5472 의견에 감사드립니다. –

5

컨트롤러 메서드에 전달 된 FormCollection 개체를 사용해 보셨습니까?

[HttpPost] 
public ActionResult Index(FormCollection formCollection) 
{ 
    foreach (string extendedProperty in formCollection) 
    { 
    if (extendedProperty.Contains("Property-")) 
    { 
     string extendedPropertyValue = formCollection[extendedProperty]; 
    } 
    } 

    ... 
} 

해당 컬렉션의 항목을 탐색하려고합니다.

관련 문제