Is there any way to check which button as caused post back in the controllfer action. My problem is I am using two buttons with no name attribute. How can i check which button has clicked when button is not having name attribute Please advice
Edit : I can't give name attribute as i am using button controls created by internal framework in that they don't have name attribute. So is 开发者_如何学Cthere any alternative for it.
I know there is one way putting those buttons in different form tags but i want to check for different options thanks
To answer your question, there is no way to check which button caused the form to be submitted without giving it a unique name.
If you can give your button a name attribute the you could simply do..
<input ... name="MyBtn" .../>
If (Request.Form["MyBtn"] != null) ...
MVC will check that for you. The submit button is connected to the Action which has the same name as the View.
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id">Id:</label>
<%= Html.TextBox("Id") %>
<%= Html.ValidationMessage("Id", "*") %>
</p>
<p>
<label for="Thema">Thema:</label>
<%= Html.TextBox("Thema") %>
<%= Html.ValidationMessage("Thema", "*") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
In the Controller you use HttpVerbs for the Get and the Post
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Add()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(ThemaWebSites thema)
{
return View(thema);
}
You can use the ID attribute if you don't want to use a name, then use the "value" attribute:
<button type="submit" name="action" value="OK" /> <button type="submit" name="action" value="Cancel" />
You need a name, otherwise it won't show up in the Request.Form
or FormCollection
and you won't be able to access the values.
Edit: if you really can't use name or ID attributes, you might be forced to use some JavaScript trickery so that each button sets the value of a hidden field on their onclick
event...
精彩评论