I want to send POST request to AdminController. But when i watch it in debugger, the request is GET.
<form method="post">
<input type="button" formmethod="post" onclick="location.href='#Url.Action("Index","Admin",new {rowID = #p.ProductID})'" value="Delete"/>
</form>
Because you wrote code to do a GET request on the submit button click !
onclick="location.href='#Url.Action("Index","Admin",new {rowID =
#p.ProductID})'"
Here you are setting the location.href value to the /Admin/Index and it will be a new GET request.
If you want to post, simply remove the onclick event on the button. If you want to send the ProductID value, you can keep that in a hidden input field inside your form and when you click submit the value of this form element will be also submitted.
#using(Html.BeginForm("Index","Admin"))
{
<input type="hidden" name="rowID" value="#p.ProductID" />
<input type="submit" value="Delete"/>
}
Assuming your HttpPost Index action method of AdminController has a parameter with same name as the input name to accept the productId.
[HttpPost]
public ActionResult Index(int rowID)
{
// to do : Return something
}
Related
I have a handler method in razor page with name OnGetExpense(UsePayload payload)
page.cshtml:
<input type="hidden" asp-for="Filters.From" value="#Model.Filters.From.ToYYYYMMDD()" />
<input type="hidden" asp-for="Filters.To" value="#Model.Filters.To.ToYYYYMMDD()" />
<a asp-page-handler="Expense" class="btn btn-primary" type="button" style="border-color: inherit">
Expense
</a>
page.cshtml.cs:
public IActionResult OnGetExpense(UserPayload filter)
{
Console.WriteLine(filter.From);
Console.WriteLine(filter.To);
return Page();
}
The From date and to date I am getting is 01-01-0001 it is not submitting the payload I needed.
Is there any tag attribute I can sen the payload with anchor tag?
Please help
Your rendered A tag will create a GET request to the endpoint, you need to to a POST to send the data in the body of the request and for the Controller to bind the Request Body to your UserPayload object.
To send the POST request you can either write some JavaScript and POST to JSON to the end point or wrap the code in a Form.
https://learn.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/4-working-with-forms
Your controller will need to have HttpPost attribute assigned.
[HttpPost]
public IActionResult OnGetExpense(UserPayload filter)
{
Console.WriteLine(filter.From);
Console.WriteLine(filter.To);
return Page();
}
Is there any tag attribute I can sen the payload with anchor tag?
You can use asp-route-{value}
Below is a work demo, you can refer to it.
cshtml:
<a asp-page-handler="Expense" asp-route-attendeeid="12" class="btn btn-primary" type="button" style="border-color: inherit">Expense</a>
cshtml.cs
public IActionResult OnGetExpense(int attendeeId)
{
return Page();
}
result:
I have a list In my view. For each row, I view button and I am passing Id value as hidden. But when I click any button it is passing wrong hidden value to the controller. Always it passes the first-row hidden value to the controller.
View:
#foreach (var list in Model)
{
<div>
<div > #( ((int)1) + #Model.IndexOf(list)).</div>
<div >#list.details</div>
<div class="col-md-2 row-index">
<button class="btn btn-link" type="submit" name="action:view" id="view">View</button>
<input type="hidden" name="viewId" id="viewId" value="list.WId" />
</div>
</div>
}
Controller:
[HttpPost]
[MultipleButton(Name = "action", Argument = "view")]
public ActionResult ViewDetail(string viewId)
{
return RedirectToAction("ViewDetails");
}
To get all values you need to change the input value type in your controller to array of strings.
I hope that this solution can help you
[HttpPost]
[MultipleButton(Name = "action", Argument = "view")]
public ActionResult ViewDetail(string[] viewId)
{
return RedirectToAction("ViewDetails");
}
if you want to get the exact value you need to duplicate the form within your foreach
in this case you should write somthing like this :
#foreach (var list in Model)
{
<div>
<div > #( ((int)1) + #Model.IndexOf(list)).</div>
<div >#list.details</div>
<div class="col-md-2 row-index">
<form ... > // complete your form attributes
<button class="btn btn-link" type="submit" name="action:view" id="view">View</button>
<input type="hidden" name="viewId" id="viewId" value="list.WId" />
</form>
</div>
</div>
}
Note : You should delete the global form
You should have one form for each row. then you submit that row.
Otherwise as you state it passes first value.
You are setting each value to the same element ID (which is invalid anyway) and name. When you submit your form (which would be more helpful to fully answer your question) it is finding the first element that matches that criteria and submitting it.
There are multiple ways to resolve this such as the already mentioned form per entry but the other preference would be to modify you button to a div and add a click handler to pass the specific value to a js function which would then submit to the controller. Its a preference choice regarding how tightly coupled you want your front end. But the main problem is your element naming convention.
In a Asp.net MVC view, i created a form, with a input field.
The user Sets a first name (or part of it), presses the submit button.
This is the form section:
<div>
<form action="SearchCustomer" methos="post">
Enter first name: <input id="Text1" name="txtFirstName" type="text" />
<br />
<input id="Submit1" type="submit" value="Search Customer" />
</form>
</div>
This is the SearchCustomer in the Controller, that gets the data from the form:
CustomerDal dal = new CustomerDal();
string searchValue = Request.Form["txtFirstName"].ToString();
List<Customer> customers = (from x in dal.Customers
where x.FirstName.Contains(searchValue)
select x).ToList<Customer>();
CustomerModelView customerModelView = new CustomerModelView();
customerModelView.Customers = customers;
return View("ShowSearch", customerModelView);
When i run the program, and enter a first name ("Jhon" for example), the code returns to SearchCustomer function, but Request.Form is empty.
Why?
Thanks.
Your method is spelled wrongly should not read methos but method like below:
<form action="SearchCustomer" method="post">
....
</form>
You need to modify your code:
you need to provide a action name here, which should be defined in your controller(SearchController) with the same name as 'ActionName' you will put in the below code.
if SearchController is your action name then provide the controller in which the action is available.
<div>
<form action="SearchCustomer/<ActionName>" method="post">
Enter first name: <input id="Text1" name="txtFirstName" type="text" />
<br />
<input id="Submit1" type="submit" value="Search Customer" />
</form>
</div>
With Html.BeginForm :
#using (Html.BeginForm("<ActionName>","<ControllerName>", FormMethod.Post))
{
Enter first name: <input id="Text1" name="txtFirstName" type="text" />
<br />
<input id="Submit1" type="submit" value="Search Customer" />
}
Set [HttpPost] on your controller.
[HttpPost]
public ActionResult SearchFunction(string txtFirstName)
{
CustomerDal dal = new CustomerDal();
string searchValue = txtFirstName;
List<Customer> customers = (from x in dal.Customers
where x.FirstName.Contains(searchValue)
select x).ToList<Customer>();
CustomerModelView customerModelView = new CustomerModelView();
customerModelView.Customers = customers;
return View("ShowSearch", customerModelView);
}
If you View is the same name as your ActionResult method, try this:
#using(Html.BeginForm())
{
... enter code
}
By default, it'll already be a POST method type and it'll be directed to the ActionResult. One thing to make sure of: You will need the [HttpPost] attribute on your ActionResult method so the form knows where to go:
[HttpPost]
public ActionResult SearchCustomer (FormCollection form)
{
// Pull from the form collection
string searchCriteria = Convert.ToString(form["txtFirstName"]);
// Or pull directly from the HttpRequest
string searchCriteria = Convert.ToString(Request["txtFirstName"]);
.. continue code
}
I hope this helps!
Can someone please tell me why the id of the checkbox 'userId' returns null on POST
<input type='checkbox' onclick='$("#userId").val("#user.Id"); return true; '/>
#using (Html.BeginFormAntiForgeryPost(Url.Action("Index", "ChangeUserAccount"), FormMethod.Post))
{
<input type="text" id="userId" />
<input type="submit" id="btn" name="btnSubmit" value="Update" style="float:right;" />
}
[HttpPost, ActionName("Index")]
[Authorize]
public ActionResult IndexPOST(UserLoginRecordIndexVM model, int? userId)
{
So on screen the text box contains the correct ID of the checkbox, but when I click the 'Update' button NULL gets returned??
Why don't you leverage this helper:
#Html.TextBox("userId", null, new { id = "userId" });
This will add the appropriate id and name attributes to your textbox.
Just add the name attribute:
<input type="text" id="userId" name="userId" />
But, also make sure your action accepts it as a parameter, string userId, or that it's part of the model that's posted back. So, in your case you might just do this:
public ActionResult IndexPOST(UserLoginRecordIndexVM model, string userId)
I have a form in my asp.net mvc view as follow:
<%using (Html.BeginForm("SearchBorrowed", "Admin", FormMethod.Get))
{ %>
<%: Html.TextBox("searchTerm", Request.QueryString["searchterm"])%>
<input type="submit" value="Search" />
<br />
Is Returned :
<%:Html.CheckBox("IsReturned")%>
<%} %>
and here is the 'SearchBorrowed' action:
public ActionResult SearchBorrowed(bool IsReturned=false, string searchTerm = null)
{
IEnumerable<BorrwoinfInfo> bs;
//...Get from repository
return View(bs.ToList());
}
and finally routing settings :
routes.MapRoute(
"SearchBorrowed", // Route name
"{controller}/{action}/{*searchTerm}", // URL with parameters
new
{
controller = "Admin",
action = "SearchBorrowed",
searchTerm = UrlParameter.Optional
} // Parameter defaults
when I submit the form without checking 'IsReturned' Checkbox,
it returns result and the url gets as follow :
.../SearchBorrowed?searchterm=&IsReturned=false
But when I check IsReturned' Checkbox, the urls gets like this:
.../SearchBorrowed?searchterm=s&IsReturned=true&IsReturned=false
Why there is two IsReturned in above url ?!
How Could I fix this ?
Why there is two IsReturned in above url ?!
Because the Html.CheckBox helper generates an additional hidden input field with the same name as the checkbox. If you look at the generated HTML you will see that the helper generated the following 2 input fields:
<input type="checkbox" name="IsReturned" id="IsReturned" value="true" checked="checked" />
<input type="hidden" name="IsReturned" id="IsReturned" value="false" />
This is by design. This helper is intended to be bound to a boolean property on your view model. When a checkbox field is not checked no value is sent to the server, so if there was not no hidden field you wouldn't be able to bind it to a boolean field.
If you don't want this hidden field you could either write a custom helper or generate the checkbox field manually.