0

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();%>
5
  • did you add the @model line on top of the View ? Commented Jul 19, 2012 at 13:20
  • <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<CustomerBook.Models.CustomerModels>>" %> @model CustomerModels <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Commented Jul 19, 2012 at 13:23
  • is this correct way and place of putting @model --shyju Commented Jul 19, 2012 at 13:24
  • which version of MVC you are using ? Any reason not to migrate to MVC3 ? Commented Jul 19, 2012 at 13:27
  • using mvc2...cnt migrate to mvc3 due to official reason.. Commented Jul 19, 2012 at 13:29

2 Answers 2

3

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.