Is it possible to know which of two submit buttons are clicked? - c#

I am thinking of having something like this:
#if(Model.ShowSubmitButton)
{
<input id="submitButton" type="submit" value="Update stuff" />
}
else
{
<input id="continueButton" type="submit" value="Continue to next page" />
}
Then in controller is it possible to know which button it is getting called from?
I originally had my controller method working with the first button, now adding a second button with different functionality, wanted to see if using the same method I can determine which button was clicked ? and if it is not possible this way, then what do you suggest?

Not without extra information. I would likely split them up rather than have two submit buttons for the same form doing two different things but if you really want to go down that road one way would be to add an additional posted variable that tells you which was clicked.
#if(Model.ShowSubmitButton)
{
<input type="hidden" name="submitStyle" value="update"/>
<input id="submitButton" type="submit" value="Update stuff" />
}
else
{
<input type="hidden" name="submitStyle" value="continue"/>
<input id="continueButton" type="submit" value="Continue to next page" />
}
To answer your additional question. Either you can add this variable to your model posted back or add it as an additional parameter.
// Where "MyModel" has a public property "SubmitStyle"
public ActionResult MyAction(MyModel model)
// Or an int, string, whatever type of parameter you want/need.
// In my example it was a string so...
public ActionResult MyAction(string submitStyle/*, <other params...>*/)

You could add a hidden field. Not ideal solution, but does the job.
#if(Model.ShowSubmitButton)
{
<input type="hidden" name="buttonClicked" value="1">
<input id="submitButton" type="submit" value="Update stuff" />
}
else
{
<input type="hidden" name="buttonClicked" value="2">
<input id="continueButton" type="submit" value="Continue to next page" />
}
In your controller you would accept it as:
public ActionResult MyAction(MyObject myobj, int buttonClicked)

There is an elegant way (tested only on chrome yet), define two inputs
<input type="submit" value="Next" name="Action"/>
<input type="submit" value="Prev" name="Action"/>
Now in you input model in controller put model defined like this:
public class MyModelDto
{
// other properties
public MyCustomAction Action { get; set; }
}
Where the
public enum MyCustomAction
{
Next=0,
Prev=1
}
Now check the value of action.

Related

How to pass different data from 2 submit buttons in 1 form

I have 2 submit buttons in one form and I want to pass data from the button that is pressed to a the server and then server can set status based on the data given by form action.
I understand the C# part, I'm just not sure with what to do in Html to pass the data from the button to the form action
HTML
<form action="#Url.Action("Create", new{ name})" method="post">
<input type="submit" class="button" name="Draft" value="Save as draft " />
<input type="submit" class="button" name="Publish" value="Save and publish" />
<form>
C#
[HttpPost]
public async Task<IActionResult> Create(string name)
{
if (name== "Draft")
{
model.Status = IPublishable._Status.Draft;
}
else
{
model.Status = IPublishable._Status.Published;
}
}
Fastest and easiest way is to add a hidden field to your form and then add something to your submit function that gives it an appropriate value (like .js). Then you will need to process that field on your action page.
Instead of doing the dynamic thing,
Just want to inform you can check this as well it's a very simple and clean approach.
I've just created a sample code for you.
//Controller side code
public class TestController : ControllerBase
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(string Name, string status)
{
return Json(status);
}
}
//View side code
<div>
<form asp-action="index" method="post">
<input type="text" name="Name" />
<button type="submit" class="button" name="status" value="1">
Save as draft
</button>
<button type="submit" class="button" name="status" value="2">
Save and publish
</button>
</form>
</div>
Here you can see the output
Just copy-paste the code to check yourself.
I have 2 submit buttons in one form and i want to pass data from the
button that is pressed to a the server and then server can set status
based on the data given by form action
Well, we can implement that using IFormCollection to handle any kind of dynamic request without defining the name and value in form. In fact, we can submit any kind of dynamic data to controller thus, we can retrive value as we posted there. You can do as following:
View:
<form action="Create" method="POST">
<input type="submit" class="button" name="Draft" value="Save as draft " />
<input type="submit" class="button" name="Publish" value="Save and publish"/>
</form>
Controller:
[HttpPost]
public async Task<IActionResult> Create(IFormCollection formData)
{
string? submittedButtonValue = formData.Keys.FirstOrDefault();
if (submittedButtonValue.Contains("Draft"))
{
model.Status = IPublishable._Status.Draft;
}
else if (submittedButtonValue.Contains("Publish"))
{
model.Status = IPublishable._Status.Published;
}
}
Note: Up to now, we can get all the data and value what we have submitted from the view.
Output:

How to send form values of array/list without correct indexing?

Let's assume that we have model and View like this:
public class Test
{
public List<string> data { get; set; }
}
<form asp-action="View">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input name="data[0]" value="dsa" />
<input name="data[1]" value="asd" />
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</form>
[HttpPost]
public IActionResult View(Test input)
{
return View();
}
I'm sending this form to the controller's method and he sees those elements of data properly.
But if I change indexing to:
<input name="data[0]" value="dsa" />
<input name="data[2]" value="asd" />
then it is not going to work. I know that it makes sense in this way, but is there an option to disable index checking and just insert all those values into list/array with correct indexing?
So, in 2nd case data[0] would be data[0], but data[1] would be data[2].
Just for every "gap" in indexing, move Next objs to fil lthat gap.
You can use just:
<input name="data[]" value="dsa" />
<input name="data[]" value="asd" />
If you specify the index at all, though, it must be in order.

Passing multi parameters from view to controller using array

I would like to post between 55 and 100 items which could be false or true from view to my controller without using Model. This is my code in Razor View:
#using(Html.BeginForm( "User","User",FormMethod.Post,new{enctype="multipart/form-data"}))
{
<input type="checkbox" name="U1">Choice one</input>
<input type="checkbox" name="U2">Choice two</input>
<input type="checkbox" name="U3">Choice three</input>
<input type="checkbox" name="U4">Choice four</input>
....
<input type="checkbox" name="U55">Choice fifty five</input>
<button type="submit">Send</button>
}
Controller:
[HttpPost]
public async Task<ActionResult> User(Array....)
{
return view();}
How can I send all of parameters (checkboxes) using an array to my controller. I appriciate all your solutions.
Dynamically you can do it like bellow code:
#using(Html.BeginForm( "User","User",FormMethod.Post,new{enctype="multipart/form-data"}))
{
<input type="checkbox" name="answer[]" value="#dynamicValue">#dynamicValue</input>
<button type="submit">Send</button>
}
and
[HttpPost]
public async Task<ActionResult> User(string[] answer)
{
}
TBH it is better to use model for binding.
However, since you explicit say you don't want to then, this would be how to do it without specific model
in your razor view, give each checkbox same name (also same as the parameter name in your action) and a value
such that:
#using(Html.BeginForm( "User","User",FormMethod.Post,new{enctype="multipart/form-data"}))
{
<input type="checkbox" name="answer" value="U1">Choice one</input>
<input type="checkbox" name="answer" value="U2">Choice two</input>
<input type="checkbox" name="answer" value="U3">Choice three</input>
<input type="checkbox" name="answer" value="U4">Choice four</input>
....
<input type="checkbox" name="answer" value="U55">Choice fifty five</input>
<button type="submit">Send</button>
}
Then in your action:
[HttpPost]
public async Task<ActionResult> User(string[] answer)
{
}
if say user checked One, two and four, then in your answer parameter you will get U1, U2 and U4
Then base on the value in answer, you can determine each question's true or false

Posting List<Object> to a webapi 2

I have created a webapi as follow
public OatherResponse Post([FromBody] List<Oather> oather)
{
Note that OatherResponse and Oather both are classes having some property.
Now I am trying to test this using simple html as below
<form method="post" action="http://localhost:50813/api/RegisterOather">
<input type="text" name="oather[0].OatherName" id="oather[0].OatherName" value="a3VsZGVlcA==" />
<input type="text" name="oather[1].OatherName" id="oather[1].OatherName" value="a3VsZGVlcA==" />
<input type="submit" value="Click Here" />
</form>
OatherName is a string property in Oather class. But I always get count of oather as zero.
Am I missing something.
The name attribute values should be
name="[0].OatherName"
name="[1].OatherName"

How to send text from the textbox to the actionResult?

I have a question about MVC4 (Razor). I have page where I want to filter data. There is a textbox (input) and a submit button. I want to move the text in the textbox to the actionresult. How can I resolve this?
On my page I've the following row:
#{ using (Html.BeginForm("Experiences"))
{
<span class="label">Filter on:</span><input id="FilterText" type="text" size="50"/>
<input type="submit" value="Submit" name="knowledge" /><br />
}
}
<br />
And I want to call the ActionResult Experiences
public ActionResult Experiences(string knowledge = "")
{
ReadExperienceData(knowledge);
return View(ListOfExperiences);
}
Specify the name property value of the input element same as the action method parameter name.
<input id="FilterText" name="knowledge " type="text" size="50"/>
<input type="submit" value="Submit" name="submitKnowledge" />
Also it looks like you are not using the Beginform method properly. You may try any of the below overloads, as needed
#using(Html.Beginform("Knowledge","YourControllerName"))
{
//form elements
}
or
#using(Html.Beginform())
{
//form elements
}

Categories