I have a partial view:
#model MsmStore.Domain.Products
<h3 class="lead">
#Model.Name
</h3>
#* <img src="~/images/MainLogo.jpg"/>*#
<p>
#Model.Decription
</p>
<p>
#Model.Price
</p>
#using (Html.BeginForm("UpdateBasketSummary", "Order", new { ProductID = Model.ID }))
{
#Html.Hidden("ProductID", Model.ID)
<input type="button" value="Click" />
}
this Html.BeginForm does not call action method (UpdateBasketSummary).
and this is my Action Method:
[HttpPost]
public PartialViewResult UpdateBasketSummary(string ProductID)
{
// orderRepository.AddToCart(ProductID, 1);
return PartialView("BasketSummary", ProductID);
}
this is my routing code:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(null, "{controller}/{action}");
An input with type button will not submit the form, by default. You need to change it to
<input type="submit" value="click" />
or
<button type="submit" value="click" />
Input of type button will not post your form, replace it with..
<input type="submit" value="Click" />
please, try this:
#using (Html.BeginForm("UpdateBasketSummary", "Order"))
{
#Html.Hidden("ProductID", Model.ID)
<input type="button" value="Click" />
}
[HttpPost]
public PartialViewResult UpdateBasketSummary(int ProductID)
{
//orderRepository.AddToCart(ProductID, 1);
return PartialView("BasketSummary", ProductID);
}
btw. not need to send ProductID in BeginForm, as you already did it by Hidden field
Edited: due to #Ben Griffiths answer:
you also need to change type='button' to type='submit' of course
Your action requires the http method POST and yet your beginForm call does not set the http method to POST. Without explicitly setting the http method you will be forced to use the default, which is a GET.
Related
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.
I'm starting to get confused. I just started building a site and stumbled upon a problem with my controller. It doesn't post at all, with or without parameters. I want to add that I just moved the ActionResult method from another controller and the View as well. It's probably something obvious but getting tired and figured I'd ask for clarification!
Viewpath/name: Views/Table/Registrations
#foreach (var registration in ViewBag.AllRegistrations)
{
using (Html.BeginForm("Registrations", "Table", new { regid = registration.RegistrationId }))
{
<tr class="odd gradeX">
<td>
<input type="submit" name="command" value="Edit" class="btn btn-default btn-xs" />
<input type="submit" name="command" value="Delete" class="btn btn-default btn-xs" />
</td>
</tr>
}
}
Controllername: TableController
public ActionResult Registrations()
{
ViewBag.AllRegistrations = registration.SelectAll();
return View();
}
[HttpPost]
public ActionResult Registrations(int regid, string command)
{
//doesn't post
return View();
}
Routeconfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Table", action = "Registrations", id = UrlParameter.Optional }
);
}
Thanks :)
[HttpPost]
public ActionResult Edit(int regid, string command)
{
//Insert some logic
}
Instead of setting the default routes you could just set the action/method name to be the same as your button "value"
This would probably make your code more readable because you could have actions/methods called create, edit, delete for instance. I hope that helps
Also where is your post logic/code? At the moment your post action is just returning a view. What are you trying to do in that method?
I have a text box that I enter data into and pass with a post to my 'home' controller on action 'results'
I want the url to end up looking like this when I post back
https://localhost:44301/Home/Results/San Francisco, CA, United States
I'm passing the text box data like this.
#using (Html.BeginForm("Results", "Home", FormMethod.Get, new { #class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarautocomplete" name="location">
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
Here is my routing.
routes.MapRoute("SearchResults",
"home/results/{location}",
new { controller = "Home", action = "Results", location = ""}
);
How do I set my routing or my form to see the data that has been submitted as location in my url?
I can get it to look like this.
https://localhost:44301/home/results?location=San+Francisco%2C+CA%2C+United+States
but I want san francisco after /results/
As #StephenMuecke mentions in the comments, you could POST your search value to a (separate) action, then redirect to your results page, passing the location as a parameter:
#using (Html.BeginForm("Search", "Home", FormMethod.Post, new { #class = "navbar-form navbar-left", role = "search" }))
{
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" id="navbarautocomplete" name="location">
<button type="submit" class="btn btn-default">Submit</button>
</div>
}
Then in your controller:
[HttpPost]
public ActionResult Search(string location)
{
return RedirectToAction("Results", new { location = location });
}
public ActionResult Results(string location)
{
return Content("location is: " + location);
}
You'll also to have the following route set up in your RouteConfig to get the friendly URL (make sure this is above the default route, as they match top-down).
routes.MapRoute(
name: "SearchResults",
url: "Home/Results/{location}",
defaults: new { controller = "Home", action = "Results" }
);
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" />
}
Using MVC 5, is it possible to have a button click event that updates a label on the same page?
For example, if I have a structure
#using (Html.BeginForm()) {
<fieldset>
<legend>Form</legend>
<p>
#Html.TextBox("textbox1")
</p>
<p>
#Html.Label("label1")
</p>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
Clicking the Submit button grabs the textbox1's value, modifies it according to the function that gets called when the submit button is clicked and then update the value of the label as the result?
Assuming my controller is called TestController.cs and this is all done on the Index page
I noticed some suggestions include using AJAX (new to me)
You don't necessarily need to use AJAX for this. All you need to do is pass the value of your label back down as part of your action result e.g.
Controller
public class TestController : Controller
{
public ActionResult Index(string label)
{
// pass label value into view
return View("Index", label ?? "");
}
[HttpPost]
public ActionResult Index(string textValue)
{
// do something with textValue
// redirect to our Index action passing the new label value
return RedirectToAction("Index", textValue);
}
}
View
#model string
#using (Html.BeginForm()) {
<fieldset>
<legend>Form</legend>
<p>
#Html.TextBox("textbox1")
</p>
<p>
#Html.Label("label", Model)
</p>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
The benefit of this approach is it follows the Post/Redirect/Get pattern so if you refreshed the page it wouldn't try to re-submit the form again.
Try this :
your cshtml code :
#using (Html.BeginForm()) {
<fieldset>
<legend>Form</legend>
<p>
<input type="text" id="textbox" name="textbox"/>
</p>
<p>
<lable id ="lable"></lable>
</p>
<p>
<input type="button" id="button" value="Submit" />
</p>
</fieldset>
}
jquery :
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
document.getElementById("lable").innerHTML = $("#textbox").val();
});
});
</script>
Demo :
http://jsfiddle.net/mgGj6/2/
Hopefully it works...!
Thanks.
You can also do it with Ajax by using
#Ajax.BeginForm();
helper. It would be more comfortable for end user.