2012-03-18 3 views
1

내 양식 필드가있는 경우 나 같은 속성 "제목"ASP MVC 3 바인딩 모델

두 개의 클래스 (제품 & 상품 검색)가 가정

public ActionResult Search(Product product) 

을하지만 결합 있도록 내가 바인드 인수를 지정할 수있는 방법이있다 : 사용 컨트롤러에 바인딩 할 수

public ActionResult Search(ProductSearch productSearch) 

나는 [Bind(Prefix = "Product")]을 사용해 보았습니다.

답변

1

[Bind(Prefix = "Product")]이 작동해야합니다. 예 :

모델 :

public class ProductSearch 
{ 
    public string Title { get; set; } 
} 

컨트롤러 :

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index([Bind(Prefix = "Product")]ProductSearch productSearch) 
    { 
     return Content(productSearch.Title); 
    } 
} 

보기 :

@using (Html.BeginForm()) 
{ 
    <input type="text" name="Product.Title" id="Product_Title" /> 
    <button type="submit">OK</button> 
} 
+0

응답 시간을내어 주셔서 감사합니다,하지만 내가 '문제를 해결하지 않습니다 ve 물었다. 나는 입력에서 Product 접두사를 제거하는 위의 문제에 대한 해결 방법이 있지만 위의 시나리오를 얻을 수 있는지 여부는 여전히 알고 있습니다. – Rob

+0

@DrRob, 네, 얻을 수 있습니다, 그것은 내 대답 주소의 첫 번째 부분입니다. –

+0

그러면이 줄을 정확히 수행하고 싶지 않은 것입니다 – Rob