Pass textbox value from view to buttons - c#

View:
#using (#Html.BeginForm("Show", "test", FormMethod.Post))
{
<fieldset>
<table >
<tr>
<td>
#Html.LabelFor(model=> model.Show)
</td>
<td>
#Html.TextBox("txtvalue", null)
</td>
</tr>
</table>
<input type="button" value="Show" onclick = "#("location.href='"+ #Url.Action("Show", "test")+"'" )" />
<input type="button" value="Go" onclick ="#("location.href='"+ #Url.Action("Go", "test")+"'" )"/>
</fieldset>
}
and two action methods in the controller,
public ActionResult Show(string txtvalue)
{
...
}
public ActionResult Go(string txtvalue)
{
...
}
based on which button been click , it should go to the corresponding action method and pass the value of the textbox.
Can anyone suggest me the way to do it. I wrapping my head around couldn't figure out.

Try this,
<input type="button" value="Show" onclick = "location.href='#Url.Action("Show", "Foo")'"/>
<input type="button" value="Go" onclick = "location.href='#Url.Action("Go", "Foo")'"/>
UPDATE:
As type="button" does not submit the values in the form, its not directly possible to do what you have asked, the better idea is to Identify the Button that has been clicked in the controller method as show in this link

Try this
<input type="button" value="Show" onclick="location.href='<%: Url.Action("Show", "Controller") %>'" />
<input type="button" value="Go" onclick="location.href='<%: Url.Action("Go", "Controller") %>'" />

Try something like:
<input type="button" value="Show" onclick="location.href='<%:Url.Action("Show", "ControllerName")%>'"/>
<input type="button" value="Go" onclick="location.href='<%:Url.Action("Go", "ControllerName")%>'"/>
If you are posting more form data you can use Ajax, see Making a Simple Ajax call to controller in asp.net mvc

Related

incorrect radio button set as "checked"

Newbie question, please be kind.
I have this Index.cshtml page in C# MVC application. When the page is rendered, the last button is checked. How do I make the first button (All) set to checked instead?
I am reading thru the "similar questions", but none seem to address my question. Please help. Thank you in advance if you can explain this to me.
<h1>#ViewBag.title</h1>
<form action="/list/index" method="post">
<h2>Search by:</h2>
<p>
#foreach (var column in ViewBag.columns)
{
<span>
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" checked="#column.Key == 'all'" />
<label for="#column.Key">#column.Value</label>
</span>
}
</p>
<p>
<label for="searchTerm">Keyword:</label>
<input type="text" name="searchTerm" id="searchTerm" />
</p>
<input type="submit" value="Search" />
</form>
<hr />
#if (ViewBag.service != null)
{
<table>
#foreach (var srv in ViewBag.service)
{
<tr>
<td>
<p>Provider: #srv.Provider.Name</p>
<p>Location: #srv.Location.Address</p>
<p>Category: #srv.Category.Name</p>
#* <p>Tag: #service.Tag</p>
<p>Keyword: #service.Keyword</p>*#
</td>
</tr>
}
</table>
}
In one condition:
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" #(column.Key == 'all' ? "checked" : " ") />
Checked or not checked?
You can just use checked for checked any radio button:
<input type="radio" checked />
For conditionally check any of your radio button, use this:
#if(column.Key == 'all'){
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" checked />
}else{
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" />
}

How to pass an object ID from a button click in asp .net core 3

I want to pass an object ID from a cardView. item.vehicleID value should be passed to the create method of a controller. When I click the button it should also route me to following page "https://localhost:44367/User/Bookings/Create".
I have my code like this,
<div class="card-columns">
#foreach (var item in Model)
{
<div class="card">
<div class="card-body">
<img class="lazyload" src="#("~/VehicleImages/"+item.vehicleImageName)" asp-append-version="true" width="100%" height="100%" />
<h5 class="card-title">#item.vehicleType</h5>
<p class="card-text">Vehicle Availability: #item.vehicleAvailability</p>
<p class="card-text">Plate Number: #item.plateNumber</p>
<p class="card-text">Price/Day: #item.vehiclePricePerDay</p>
<p class="card-text">Transmission: #item.transmission</p>
<input type="submit" class="btn btn-info form-control" value="Book" onclick="location.href='#Url.Action("Areas=User", "Create", "BookingsController", new { id = item.vehicleID })'" />
</div>
</div>
}
</div>
What I actually want is, when I click the input button, I want to get the item.vehicleID and pass the ID value to the Create method in the BookingsController that I'm calling which is located in the User Folder and this User Folder is located in the Areas Folder.
The below code lines shows how I tried,
<input type="submit" class="btn btn-info form-control" value="Book" onclick="location.href='#Url.Action("Areas=User", "Create", "BookingsController", new { id = item.vehicleID })'" />
<input type="submit" class="btn btn-info form-control" value="Book" onclick="location.href='#Url.Action("Create", "BookingsController", "User", new { id = item.vehicleID })'" />
<input type="submit" asp-controller="Bookings" asp-area="User" asp-action="Create" class="btn btn-info form-control" value="Book" itemid="vehicleID" />
Currently I tried the above code lines but it doesn't load the page or anything. So what I actually want is to direct to this link "https://localhost:44367/User/Bookings/Create" when I click the button and it also should pass the vehicleID from item.vehicleID to the Create method of the BookingsController from that button click.

How to pass textbox values to MVC Action?

I have tried the below but I get the compliation error:
The name 'txtMakeModelTypeCode' does not exist in the current context.
<label>Type Code</label>
<input type="text" id="txtMakeModelTypeCode" name="txtMakeModelTypeCode" />
<label>Make</label>
<input type="text" id="txtMake" name="txtMake"/>
<label>Model</label>
<input type="text" id="txtModel" name="txtModel"/>
Create
I have also tried using a form but I don't want to use a submit button. Is it possible to use a plain button?
#using (Html.BeginForm("CreateMakeModel", "Vehicle"))
{
<label>Type Code</label>
#Html.TextBox("txtMakeModelTypeCode")
<label>Make</label>
#Html.TextBox("txtMake")
<label>Model</label>
#Html.TextBox("txtModel")
<input type="submit" value="Create" />
}
MVC does not work the same as ASP.Net Webforms. The textboxes you create are not available in code. You use the controls to render your html and handling of data is done via the model.
So use TextBoxFor:
#using (Html.BeginForm("CreateMakeModel", "Vehicle"))
{
<label>Type Code</label>
#Html.TextBoxFor(m => m.MakeModelTypeCode)
<label>Make</label>
#Html.TextBoxFor(m => m.Make)
<label>Model</label>
#Html.TextBoxFor(m => m.Model)
<input type="submit" value="Create" />
}
Then in your controller, after the post, you should have the posted data in the model:
public ActionResult Index(CreateMakeModel model) // or is it Vehicle?
{
// whatever you do here:
string code = model.MakeModelTypeCode;
}

ASP.NET MVC: Why request is being caught by the wrong method?

I'm using to wrap my forms the following helper:
#using (Html.BeginForm("Edit", "MyController", FormMethod.Post)) { ... }
In my Controller I have two methods, one for loading my partial view and another one for processing the Post request:
[SomeFilter]
[ChildActionOnly]
[AcceptVerbs(HttpVerbs.Get)]
public PartialViewResult Edit(int id)
{
//Some Code
}
[SomeFilter]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(MyViewModel model, string submit) {
//Some Code
}
Everything seems to be working fine except when users submit an empty form. In that case request is being caught by GET Method instead of POST one. I know it's calling the GET method because I get an exception as:
"The action 'Edit' is accessible only by a child request."
And only the GET overload has [ChildActionOnly] filter. I don't understand why is this happening. Both are different and both are decorated.
Any suggestions?
Partial View code:
#model MVC.Models.MyViewModel
#using (Html.BeginForm("Edit", "MyController", FormMethod.Post))
{
#Html.HiddenFor(m => m.Id)
#Html.AntiForgeryToken()
<div class="row margin-top-20 form-group text-center">
<div class="col-md-3 col-lg-offset-2">
#Html.LabelFor(m => m.ManyItemsAvailable)
#Html.ListBox("ManyItemsAvailable", Model.ItemsAvailable)
</div>
<input type="submit" class="btn btn-default" value=">" id="add" name="submit" />
<input type="submit" class="btn btn-default margin-top-10" value="<" id="remove" name="submit" />
<div class="col-md-3">
#Html.LabelFor(m => m.ManyItemsSelected)
#Html.ListBox("ManyItemsSelected", Model.ItemsSelected)
</div>
</div>
}
So, I replaced both submit buttons with these:
<input type="submit" class="btn btn-default" value=">" id="add" name="submit" />
<input type="submit" class="btn btn-default margin-top-10" value="<" id="remove" name="submit" formmethod="post" />
Explicitly specifying formmethod="post" and it's now working as expected.

How to show/hide HTML Buttons via MVC Action Method

In my web page, I need to populate button according to parameter value called ButtonType.
let's say that If ButtonType == "Edit" then I need to hide every buttons but butUpdate.
I want to know how to show/hide html buttons via MVC Action method.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SupplierDetail(int SupplierID, string ButtonType)
{
var Supplier = supplierListRepository.Supplier_SelectByID(SupplierID);
return View(Supplier);
}
I am using Asp.net Mvc Razor form.
#using (Html.BeginForm("SupplierDetail_SubmitClick", "Supplier", FormMethod.Post, new { id = "frmSupplierDetail" }))
{
#Html.ValidationSummary(true)
<table cellpadding="0" cellspacing="0" border="0" style="width:450px; height:auto">
.....
<tr>
<td>#Html.LabelFor(model => model.Phone)</td>
<td>#Html.EditorFor(model => model.Phone)
#Html.ValidationMessageFor(model => model.Phone)</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" id="butSave" name="butSave" value="Save" style="width:100px; height:auto" />
<input type="submit" id="butUpdate" name="butUpdate" value="Update" style="width:100px; height:auto" />
<input type="submit" id="butDelete" name="butDelete" value="Delete" style="width:100px; height:auto" />
<input type="submit" id="butReset" name="butReset" value="Reset" style="width:100px; height:auto" />
</td>
</tr>
</table>
</div>
<div id="content">
#Html.ActionLink("Back to List", "Index")
</div>
}
Every Suggestions will be appreciated.
It is not a controller action responsibility to show/hide buttons. A controller action doesn't/shouldn't even know what a button means. That's a concept that exists on the view. A controller action on the other hand is supposed to communicate with the model and prepare a view model that it passes to the view for displaying. So you could define a view model that will contain properties defining the visibility of the buttons and based on the value of the ButtonType parameter set those properties accordingly. Then the controller action will pas this view model to the view instead of the supplier object that you are currently passing. Obviously the view model will also have a property to hold this supplier. Now all that's left for the view is based on the values of the view model properties decide how to display the buttons.
in your controller add the buttontype to viewdata:
ViewData["ButtonType"] = ButtonType
Then, in the view itself, you can add if/else statements, or any other logic that suits all of ur cases, to decide what to render:
#if (ViewData["ButtonType"].ToString() == "Edit")
{
<input type="submit" id="butUpdate" name="butUpdate" value="Update"
style="width:100px; height:auto" />
}
Of course, this is but a demo of what can be done, Yuo should adapt the code to ur buisness logics

Categories