2012-03-18 12 views
1

私は私のフォームのフィールドを持っている場合、私は同じプロパティ「タイトル」ASP MVC 3バインドモデル

を持つ2つのクラス(製品& Googleショッピングを)持っていると仮定すると、

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

対応するための時間を割いてくれてありがとう、それは私が「の質問に対処していません尋ねた。私は上記の問題を回避するために入力からProductプレフィックスを取り除いていますが、私はまだ上記のシナリオが得られるかどうかを知りたいと思っています。 – Rob

+0

@DrRob、はい、それは入手可能です、それは私の答えの最初の部分が扱っているものです。 –

+0

私はこの行をこれは私がやりたくないものです – Rob