I have a simple Web Application in C#.
In Index.cshtml file, I want to insert a button, and when I click on it, call a method of my Controller.
So I wrote this code:
<div class="row" style="float:left;width:100%;height:100%;">
<div class="col-md-12" style="width:100%;height:100%;">
#using (Html.BeginForm("action", "IndexController"))
{
<input type="submit" value="Create" />
}
</div>
</div>
In my controller I'm build this code:
[HttpPost]
public ActionResult MyAction(string button)
{
return View("TestView");
}
But if I try to click on button the application does not call method MyAction.
ASP.NET MVC convention requires the action name be used and the controller name minus "Controller" so, since your action method's name is "MyAction" and your controller's name is "IndexController", update the call like this:
#using (Html.BeginForm("MyAction", "Index"))
Update based on comment:
To send text to your action method to satisfy the string button parameter, you can include an input field in your form like this:
#using (Html.BeginForm("MyAction", "Index"))
{
<input type="text" name="button"/>
<input type="submit" value="Create" />
}
note that the name attribute of the text field is "button" to match the string button parameter name.
i'd suggest that another name other than button might be good, but whatever the name of your parameter, the input field that has a matching name attribute will supply the value to that parameter.
Related
I have a form in C# ASP, and two buttons submit with different path.
<form asp-action="">
<button type="submit" data-action="Liquidation" class="btn-method-switchable">
<button type="submit" data-action="Return" class="btn-method-switchable">
I try to modify the path with Jquery like that :
$('.btn-method-switchable').click(function(e) {
e.preventDefault();
let value = $(this).data('action')
$('form').attr('asp-action', value);
$('form').submit();
})
I enter in my Controller but not on the method Liquidation or Return.
Thanks for your answers
The asp-action attribute is only used before rendering. When the page is rendered it's converted to a standard HTML form action attribute.
Also note that on the client side its value is converted from the reference to the action name to a relative URL to the route. More detail on that in the documentation.
Therefore to correct your code you need to set the action attribute and also amend data-action on the button elements to be the full route. This can be done using the Url.Action() helper. The code would look something like this:
<form>
<button type="submit" data-action="#Url.Action("Liquidation")" class="btn-method-switchable">Foo</button>
<button type="submit" data-action="#Url.Action("Return")" class="btn-method-switchable">Bar</button>
</form>
$('.btn-method-switchable').click(function(e) {
e.preventDefault();
let value = $(this).data('action')
$('form').attr('action', value).submit();
})
I am passing the input data from the .cshtml page to the action method.
Here is my .cshtml page:
#model passingdata
<form asp-controller="Home" method="post" asp-action="About" >
<button class="btn-danger" type="submit" value="Submit"></button>
<div class="form-group">
<label>
<input asp-for="date" placeholder="Date" class="col-md-8" />
</label>
<label>
Select from these Four Classes
<input type="radio" name="class" asp-for="classselect1" id="classselect1" value="classselect1" class="col-md-4" /> <p> #Model.classname1</p>
<input type="radio" name="class" asp-for="classselect2" id="classselect2" value="classselect2" class="col-md-4" /> <p> #Model.classname2</p>
<input type="radio" name="class" asp-for="classselect3" id="classselect3" value="classselect3" class="col-md-4" /> <p> #Model.classname3</p>
<input type="radio" name="class" asp-for="classselect4" id="classselect4" value="classselect4" class="col-md-4" /> <p> #Model.classname4</p>
</label>
</div>
</form>
And here is my controller code which is invoked when i Click on the button.
[HttpPost]
public IActionResult About(passingdata p)
{
ViewData["Message"] = "Your application description page.";
Teacher.classselect1 = p.classselect1;
Teacher.classselect2 = p.classselect2;
Teacher.classselect3 = p.classselect3;
Teacher.classselect4 = p.classselect4;
Teacher.date = p.date;
return View();
}
The input data like date and bool value from the radiobutton is not passing through the object of the class which contain these variables.
Please help me in this.
If i remember correctly, the Name attribute of each radiobutton is how .net MVC will map the values to your model.
In this case, all of your names are "class", which essentially means you have 1 form field named class with 4 options.
I would recommend using the html helper classes, because they will automatically create the proper html for you. The answer to this post should help: When using .net MVC RadioButtonFor(), how do you group so only one selection can be made?
If you dont want to use the helper just remember that when you submit a form, the data that is posted is based on the name of each form field. .Net does some magic in the background to serialize your Model for you, but essentially you are just submitting data in the format "?prop1=val1&prop2=val2".
Update
I figure maybe I should clarify a little better why what you are doing is not working how you expect.
When you post or put data via a form, it passes the input fields (text box, radio button, checkbox, etc...) as either querystring params or are part of the body. Radio buttons work a little differently than other input type. For a radio button, there are multiple input elements, but only one of them is valid. That is handled by using the name attribute. In your case, all of the names are "class", which means that the only thing being passed to the server is a single "?class={val}" (val is the value of which ever radio button is selected).
If your passingdata model had a property called "class", it would be populated. If your goal is to populated all 4 of the classselect properties with different values, you would need the name of each radio button to be different. But if there was only one radio button with each name, then each property could only have 1 value. You would need multiple RadioButtons with the same name to have multiple values (only one of which is selectable for each property).
Hopefully that clarifies what is wrong and gets you in the right direction.
I have a view that is created within a MVC area.
from this view i want to add an Html form that gets to an action on a controller that is not in any Area but just in my main controller folder.
#using (Html.BeginForm("MyAction", "MyController", FormMethod.Get))
{
<input type="submit" value="TEST" />
}
When i execute this code it tries to find my controller in the same area and fails, following html is generated
<form action="/MyApp/en/MyController/MyMethod" method="get">
this actually works on other calls:
http://localhost:18183/MyApp/nl/OtherController/OtherAction
My action on the controller without area can be reached in the browser like this:
http://localhost:18183/MyController/MyMethod
But when i add the form the classic way it still doesn't work:
<form action="/MyController/MyMethod" method="get">
How can i achieve to call this controller?
You need to specify an annonymous object to the routeValues attribute:
#using (Html.BeginForm("MyAction", "MyController", new { area = "" }, FormMethod.Get))
{
}
If you wanted to go to a different area rather than no area, modify the empty quotes to be your new area
Documentation for this overload is available at http://msdn.microsoft.com/en-us/library/dd492933(v=vs.118).aspx
In a Razor view I have input type="text", a hidden field and a button. I can access hidden field from Form collection but its weird I cant access input type="text" value inside my action. I am not sure if my understanding is correct or not but I was thinking as all fields inside form should be available inside action.
Below is my code please:
#using (Html.BeginForm())
{
<div style="margin-top: 40px;">
<input id="txtDateFrom" class="span2" size="16" value="#Model.StartDate.ToString("dd/MM/yyyy") " readonly="readonly" type="text">
#Html.Hidden("currencyCode", (object)ViewBag.currencyCode)
</div>
<button onclick="#Url.Action("ExchangeRateDetails", "ExchangeRate")" class="btn btn-lg span2 ARML50px">
}
I highly appreciate your time, guidance and help.
The reason your hidden input works is that you render this with help from the Html helper #Html.Hidden. This helper render the input field with the name attribute.
Your <input type="text"> is missing the name attribute. So try writing like this:
<input id="txtDateFrom" name="txtDateFrom" class="span2" size="16" value="#Model.StartDate.ToString("dd/MM/yyyy") " readonly="readonly" type="text" />
The name="txtDateFrom" will make the value appear in your FormCollection.
Try this way
You don't need onclick="#Url.Action("ExchangeRateDetails", "ExchangeRate")" to the button
Change your button for below way
<button type="submit" class="btn btn-lg span2 ARML50px">
And Now your controller can get the input text
[HttpPost]
Public ActionResult ExchangeRateDetails(YourmodelClass xxx)
{
string dates=Model.StartDate;
}
This website have lot and lot of answers for how to send a model values from view to controllers by see the Related discussion on this page right corner .
I have a view with a textbox and a button. I want to take the textbox and take the contents of what a user types in and put it as a "Get" variable in the URL. Does anyone have a simple example of this?
I want it to print into a url like this: /Profiles/Search?searchstring=hello
I am using razor built in mvc3 view
If you're using a strongly typed view, you could do:
#using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Get))
{
#Html.TextBoxFor(m => m.SearchString)
<input type="submit" value="Search" />
}
Otherwise, with a weakly typed view:
#using(Html.BeginForm("ActionName", "ControllerName", FormMethod.Get))
{
#Html.TextBox("searchstring")
<input type="submit" value="Search" />
}