0

Okay I'm new to MVC and trying to make me a webpage where I can transfer to small form box with a zip code and button, to quote page that will fill in the zip code part on the quote page.

My problem is I have two controllers a homeController that has a index view with a small form box. I need to pass the zip code to the QuoteController which has it's own view that populated with the new zip code.

the home controller input, indexview

 @using (Html.BeginForm("Quote", "Quote"))    
  <p>Move From Zip:</p>  
 <input type="text" name="Zip"/><br /> 
 <input type="submit" value="Next" name="next">

the quote form to receive the zip, on the quote controller, on the quote view

@Html.Label("Move From Zip ")<br />
@Html.TextBoxFor(m => m.MoveFromZip, "", new { maxlength = 5, @class =    "short-textbox" })

what's the easiest way of doing this

1 Answers1

1

In your Index view of HomeController, you can keep the form action to "Quote/Quote"

@using (Html.BeginForm("Quote", "Quote"))
{
  <input type="text" name="Zip" />
  <input type="submit" />
}

Create a view model for your Quote action method view in QuoteController

public class QuoteVm
{
  public string Zip { set;get;
}

and in your QuoteController's Quote action method

[HttpPost]
public ActionResult Quote(QuoteVm model)
{
  return View(model);
}

and your Quote view will be

@model QuoteVm
<p>Data passed(POSTED) from Index view</p>
@using(Html.BeginForm("QuoteSave","Quote"))
{  
  @Html.TextBoxFor(s=>s.Zip)
  <input type="submit" />
}

Now for your form submit in this view, you need to have another HttpPost action method

[HttpPost]
public ActionResult QuoteSave(QuoteVm model)
{
   // to do : Do something and return something
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thanks it worked, but I don't know why? I only used the second block of code you provided inside the current QuoteModel class where my validation was, then all I add was the items in the begin form method, and it worked the other stuff had no effect on the transfer. –  Feb 06 '16 at 00:09
  • There is one other issue though, when the data is transferred to the quote page it triggers all my validation, how do I prevent that. –  Feb 06 '16 at 00:10
  • @NickBooth I don't know if your edit will go through but in the future you should point out that you're fixing a typo (the missing brace in the 2nd code block) ideally with a link to back that up. – BSMP Aug 13 '19 at 18:02