2011-08-23 5 views
1

MVC2에서 강력한 형식의 도우미를 사용하는 경우 게시물을 만들 때 입력 필드 값이 Model 속성에서 가져 오지 않습니다. 이 기본 동작입니까?asp.net-mvc2 - 모델을 사용하지 않는 강력한 유형의 도우미?

강력한 형식의 도우미와 (강력한 형식)보기 :

<div class="editor-label"> 
    <%: Html.LabelFor(model => model.Name) %> 
</div> 
<div class="editor-field"> 
    <%: Html.TextBoxFor(model => model.Name) %> 
    <%: Html.ValidationMessageFor(model => model.Name) %> 
</div> 

<div class="editor-label"> 
    <%: Html.LabelFor(model => model.Price) %> 
</div> 
<div class="editor-field"> 
    <%: Html.TextBoxFor(model => model.Price) %> 
    <%: Html.ValidationMessageFor(model => model.Price) %> 
</div> 

컨트롤러의 행동은 :/제품/편집/5

public ActionResult Edit(int id) 
    { 
     var p = new Product(); 
     p.Name = "product 1"; 
     p.Price = "100"; 
     return View(p); 
    } 

HTML을 출력 :

<div class="editor-label"> 
    <label for="Name">Name</label> 
</div> 

<div class="editor-field"> 
    <input id="Name" name="Name" type="text" value="product 1" /> 
</div> 

<div class="editor-label"> 
    <label for="Price">Price</label> 
</div> 
<div class="editor-field"> 
    <input id="Price" name="Price" type="text" value="100" /> 
</div> 
,

컨트롤러 조치 :/제품/편집/5

[HttpPost] 
    public ActionResult Edit(Product p) 
    { 
     p.Name = "prrrrrrd 2"; 
     return View(p); 

    } 
내가 prrrrrrd 2. 어디 않습니다 "로 ID ="이름 "과 입력 값을 기대 아래 양식 포스트 (후

HTML 출력 강력한 형식의 도우미는에서 값의 수) :

<div class="editor-label"> 
    <label for="Name">Name</label> 
</div> 

<div class="editor-field"> 
    <input id="Name" name="Name" type="text" value="product 1" /> 
</div> 

<div class="editor-label"> 
    <label for="Price">Price</label> 
</div> 
<div class="editor-field"> 
    <input id="Price" name="Price" type="text" value="100" /> 
</div> 

답변

4

입력 필드 게시물이 이루어질 때 이 모델 속성에서 촬영되지 않은 값 MVC2에 강력한 형식의 헬퍼를 사용. 이게 기본 동작입니까?

예, 먼저 모델 상태에서 가져온 다음 모델에서 가져옵니다. POST 액션에서 모델에 대한 수정 작업을 수행하려면 먼저 ModelState에서 제거해야합니다. 예 :

[HttpPost] 
public ActionResult Edit(Product p) 
{ 
    ModelState.Remove("Name"); 
    p.Name = "prrrrrrd 2"; 
    return View(p); 
} 
+0

이전 프로젝트에서는이 점을 기억하지 못합니다. ModelState가 비활성화되었거나 이전 '릴리스'의 어떤 것이 가능합니까? – ReFocus

+0

@ReFocus, 이전 프로젝트가 의미하는 것이 확실하지 않습니다 *. ASP.NET MVC 1.0을 의미하는 경우 강력하게 형식화 된 도우미 ('For'로 끝나고 첫 번째 인수로 람다 식을 사용)가 없습니다. –

+0

방금 ​​두 번 확인했습니다. '기본'도우미는 모델 값보다 먼저 ModelState를 사용합니다. 이 동작은 MVC2에서 새로 추가 되었습니까? – ReFocus

관련 문제