Pass argument to controller on submit - asp.net-mvc

So, i started learning MVC, and i need to pass an email to a controller. (Trying to make a standard email signup)
Therefore i have an input and a button which (should) pass the input to an argument accepting controller and then redirect to another view.
I have the following controllers:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string mail)
{
person = new EmailSignup{Email = mail};
return RedirectToAction("details");
}
public ActionResult details()
{
return View(person);
}
This is what i have in my View:
#using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<form class="col-md-12">
<div class="form-group form-inline">
<label class="margin20">Sign up for newsletter</label>
<input type="Email" class="form-control" style="display:inline-block; max-width:200px" id="mail" placeholder="Example#Example.com" />
<button type="submit" class="btn btn-default" style="display:inline-block" id="emailSignup">Signup</button>
</div>
</form>
}
It redirects to my "details" view, but my email is not showing.
Furthermore, is this best practice? would i want to do it like this?

#using (Html.BeginForm("Index", "Home", FormMethod.Post)) renders a form, you don't need a second one inside it (if you need to add the class, you can use an overload of Html.BeginForm). Your input contains an id property, but not a name property. name is what's used when an action happens inside a form.

Related

How can i invoke a method in a different controller including parameters using mvc 6

I'm new to developing web applications and most importantly new to mvc. I'm trying to navigate from one view to another controller action including parameters. I have the below code in my currently displaying view:
<p>
<a asp-controller="Working_set" asp-action="Create">Create new Working set</a>
</p>
#foreach (var item in Model)
{
<div class="col-sm-6 col-md-3">
<div class="thumbnail tile tile-medium">
<a asp-controller="SelectedWorking_set" asp-action="index">
<h2>
#Html.DisplayFor(modelItem => item.Name)
<input name="workingSetID" type="hidden" value="#item.Working_setID" />
</h2>
</a>
</div>
</div>
}
How can i use the Working_setID in the controller SelectedWorking_set below:
[Route("SelectedWorking_set")]
public class SelectedWorking_setController: Controller
{
private FlightmapContext _context;
public SelectedWorking_setController(FlightmapContext context)
{
_context = context;
}
public IActionResult Index()
{
return View();
}
[HttpPost("Index")]
public IActionResult Index([FromBody]int workingSetID)
{
//return View(_context.Project.ToList());
return View();
}
}
You need to pass input name to your controller method like below example;
<input name="workingSetID" type="hidden" value="#item.Working_setID" />
here "workingSetID" name we need to pass your controller method, Suppose your method name is "Index" then you need to write like this in your controller.
[HttpPost]
public ActionResult Index(string workingSetID)
{
//Code here
}
like this you will get "Working_setID" value in your controller method. And also you need submit button to post this value to controller.

Passing form values from one controller view to another form on a different controllers view in asp MVC

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

Update and ASP.NET MVC model on button click

I'm new to ASP.NET MVC. I'm trying to update model on button click with no success: every time I push the button an HttpGet controller method is invoked.
Here is my markup
#model DataInterface.Model.Entry
<button onclick="location.href='#Url.Action("Survey")'">Finish survey</button>
Here is Controller code
[HttpGet]
public ActionResult Survey()
{
var entry = new Entry();
return View(entry);
}
[HttpPost]
public ActionResult Survey(Entry newEntry)
{
// save newEntry to database
}
When I click button HttpGet method is invoked. Why?
It is bad to be a rookie)
Thanks to all!
If you access a URL without explicitly specifying the HTTP method, ASP.NET MVC will assume a GET request. To change this, you can add a form and send it:
#using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
<input type="submit" value="Finish survey" />
}
If you do this, your POST method will be invoked. The Entry parameter, however, will be empty, since you do not specify any values to send along with the request. The easiest way to do so is by specifying input fields, e.g. text inputs, dropdown, checkboxes etc.
#using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
#Html.TextBoxFor(m => m.Title)
<input type="submit" value="Finish survey" />
}
If you have the object stored on the server somewhere and only want to finish it off by writing it into the database or changing its status, you could pass the Id of the object (or some temporary Id) along the post request and make the controller method work only with the Id:
#using (Html.BeginForm("Survey", "Controller", FormMethod.Post))
{
#Html.HiddenFor(m => m.Id)
<input type="submit" value="Finish survey" />
}
[HttpPost]
public ActionResult Survey(Entry newEntry)
{
// newEntry.Id will be set here
}
#using (Html.BeginForm("Survey", "<ControllerName>", FormMethod.Post))
{
<input type="submit" value="Finish survey" />
}
you must declare your form
#model DataInterface.Model.Entry
#using (Html.BeginForm("action", "Controlleur", FormMethod.Post, new {#class = "form", id = "RequestForm" }))
{
<input type="submit" value="Finish survey" />
}

connecting an action of button in view in asp.net mvc3

I am beginner in asp.net mvc . I am going to connect an action of button in view.
but i cant. i work with web form, in fact i want to click on the button, create action will be called and insert the data.
my code is as following:
The Controller:
namespace BookStore.Controllers
{
public class BookController : Controller
{
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(book bookobj)
{
var dBook=new DBook();
dBook.Insertbook(bookobj);
return RedirectToAction("Index", "Home");
}
}
}
The View:
#model BookStore.Models.DomainObject.book
#{
ViewBag.Title = "Create";
}
<h2>insert data/h2>
#using(Html.BeginForm())
{
#Html.ValidationSummary(true);
<fieldset>
<div>
#Html.LabelFor(model=> model.book_name)
</div>
<div>
#Html.EditorFor(model => model.book_name)
</div>
<div>
#Html.LabelFor(model=>model.book_qty)
</div>
<div>
#Html.EditorFor(model=>model.book_qty)
</div>
<br/>
<div>
<input id="Button_craete_book" type="button" value="insert" />
</div>
</fieldset>
}
Change the button type to "submit":
<input id="Button_craete_book" type="submit" value="insert" />
That will post the form and values to the Edit method in the controller, as marked with the HttpPost attribute.

Get value of view element in MVC that not relevant to model

When I have a DropDownList that relevant to Model of view like this:
#Html.DropDownListFor(model => model.Group.Name, selectList)
I Can retrieve Values in controller as follow:
string SelectedGroupName = collection.GetValue("Group.Name").AttemptedValue;
But now I have a DropDownList that not relevant to model but I need the value of that, this is my new DropDown:
#Html.DropDownList("DDName", selectList)
How can I retrieve the selected value of that in controller? is there any hiddenfield or other thing to pass value from view to controller?
Edit
This is my view:
#model PhoneBook.Models.Numbers
#{
ViewBag.Title = "Delete";
}
<h2>
Move And Delete</h2>
<fieldset>
<legend>Label of Numbers</legend>
<div class="display-label">
Delete Label And Move All Numbers with: #Html.DisplayFor(model =>
model.Title)</div>
<div class="display-field">
To #Html.DropDownList("DDName", selectlist)
</div>
</fieldset>
#using (Html.BeginForm()) {
<p>
<input type="submit" value="Move Numbers And Delete Label" name="MDbtn" />
</p>
}
This is my Controller:
[HttpPost]
public ActionResult Delete(int id, FormCollection collection) {
var result = Request["DDName"];
//Use result
return RedirectToAction("Index");
}
but result set to null, why?
I think this must be work:
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
var dd = collection.GetValue("DDName");
.....
}
I think all you have to do is
In your view:
put #using (Html.BeginForm()) { above the <fieldset>, so the #Html.DropDownList("DDName", selectlist) is inside it.
In your controller:
public ActionResult Delete(int id, FormCollection collection, string DDName)
{ [...] }
And I'm fairly sure MVC3 will automagically give you the selected value as parameter to your controller.
If that does not work, try object DDName in your controller instead.
Think the ddl has to be in your form if you want to pass the value in through the form collection
Your problem is, that your dropdown isn't contained inside the form in your view.
You have to put it after BeginForm:
#using (Html.BeginForm()) {
<div class="display-field">
To #Html.DropDownList("DDName", selectlist)
</div>
<p>
<input type="submit" value="Move Numbers And Delete Label" name="MDbtn" />
</p>
}
Then you have can use FormCollection or a designated parameter. The default Modelbinder will work with both approaches:
ActionResult Action (FormCollection collection, string DDName)
You can easily check those issues with fiddler.

Resources