0

これが私の見解です

<form method="post" action="/LoadCustomerAndDisplay/Search">
<fieldset>
    <legend>Customer Book</legend>
    <%= Html.Label("Name") %>

    <%: Html.TextBox("Name") %>
    <br />
    <br />
    <div>
        <input type="submit" value="Sign" />
    </div>
</fieldset>
</form>

これは私のコントローラーです...

 public ActionResult Search() 
    {
        CustomerModels objCustomer = new CustomerModels();
        var dataval = objCustomer.getData();
        return View(dataval);

}

コントローラでNameテキストボックスの値を取得し、このようにgetDataに渡すにはどうすればよいですか。

 var dataval = objCustomer.getData(ViewData['Name']);

これは私が置いた...fnameにエラーを表示しています....ディレクティブの追加がありません....今の問題は何ですか...

 <% Html.BeginForm("Search", "LoadCustomerAndDisplay");%>
    <%: Html.TextBoxFor(m => m.fname) %>
    <p>
        <button type="submit">
            Save</button></p>
    <% Html.EndForm();%>
4

2 に答える 2

3

強く型付けされたビューを使用します。GETアクションメソッドで、ViewModelのオブジェクトをビューに渡し、HTMLヘルパーメソッドを使用して入力要素を作成します。POSTフォームを送信すると、MVCモデルのバインドにより、アクションメソッドのViewModelのプロパティ値として値が取得されます。

GETアクションは同じままです

public ActionResult Search() 
{
    CustomerModels objCustomer = new CustomerModels();
    var dataval = objCustomer.getData(); 
    // Assuming this method returns the CustomerViewModel object 
    //and we will pass that to the view.

    return View(dataval);
}

だからあなたのビューは次のようになります

@model CustomerViewModel
@using (Html.BeginForm())
{
  @Html.LabelFor(x=>x.Name)
  @Html.TextBoxFor(x=>x.Name)
  <input type="submit" value="Save" /> 
}

そして、これを処理するためのPOSTアクションメソッドがあります

[HttpPost]
public ActionResult Search(CustomerViewModel model)
{
  if(ModelState.IsValid)
  {
    string name= model.Name;

   //  you may save and redirect here (PRG pattern)
  }
  return View(model);

}

objCustomer.getData()GET Actionメソッドのメソッドが、次のようなプロパティCustomerViewModel を持つオブジェクトを返すと仮定しますName

public class CustomerViewModel
{
  public string Name { set;get;}
  //other properties as needed
}
于 2012-07-19T12:58:30.007 に答える
0

TypeCustomerModelsのオブジェクトを受け入れるパラメータを検索アクションに追加できます。そうすれば、何かをコントローラーにポストバックすると、モデルバインダーはフォームからデータを取得し、CustomerModelsタイプのオブジェクトを生成します。このオブジェクトは、アクションで使用できます。そのためには、2つのことを行う必要があります。

  1. ビューは、タイプCustomerModelsのモデルを受け取る必要があります
  2. アクションは、次のようなものにする必要がありますpublic ActionResult Search(CustomerModels model)

ビューを変更したくない場合、つまりモデルをページに渡したくない場合は、コントローラー内でTryUpdateModelを試して使用するか、FormCollectionオブジェクトを検索アクションに渡してからそのコレクションをクエリします。

于 2012-07-19T13:00:00.137 に答える