passing value from view to controller in MVC

半城伤御伤魂 提交于 2019-12-25 00:16:27

问题


This is my view

<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>

This is my controller...

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

}

How can i get the value of Name textbox in the controller and pass it to the the getData like this....

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

this i put...showing error on fname....missing adding directive....what's the issue now...

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

回答1:


Use strongly typed view. In your GET action method, pass an object of your ViewModel to the view and use the HTML helper methods to create the input elements. When you submit the form, due to MVC model binding, you will get the values as the property values of the ViewModel in the POST action method.

Your GET action can stay same

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);
}

so your View will be like

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

And have a POST action method to handle this

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

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

}

Assuming your objCustomer.getData() method in your GET Action method returns an object of CustomerViewModel which has a Name property like this

public class CustomerViewModel
{
  public string Name { set;get;}
  //other properties as needed
}



回答2:


You can add a parameter to your Search action that accepts an object of Type CustomerModels. That way when you post something back to the controller, the model binder will take the data from the form and generate an object of type CustomerModels which you can then use in your action to work with. For that you need to do two things:

  1. Your view should receive a model of type CustomerModels
  2. Your action should be something like this public ActionResult Search(CustomerModels model)

If you don't want to change your view, that is, you don't want to pass model to your page, you could try and use TryUpdateModel inside your controller, or pass FormCollection object to your Search action and then query that collection.



来源:https://stackoverflow.com/questions/11561325/passing-value-from-view-to-controller-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!