Using buttons in MVC3
In my view I am using buttons like this
<button style=" color:White; height:30px; width:100px; background-image: url('@Url.Content("~/Content/Images/RedButton.gif")');" name="button" value="Add" type="submit">Add</button>
<button style=" color:White; height:30px; width:100px; background-image: url('@Url.Content("~/Content/Images/RedButton.gif")');" name="button" value="Save" type="submit">Save</button>
<button style=" color:White; height:30px; width:100px; background-image: url('@Url.Content("~/Content/Images/RedButton.gif")');" name="button" value="Cancel" type="submit">Cancel</button>
and handling them in the controller like this
[HttpPost]
public ActionResult Index(BuildRegionModel model, string button)
{
if (button == "Add")
{
}
if (button == "Save")
{
}
if (button == "Cancel")
{
}
return View(model);
}
开发者_开发知识库Is there a way that I can do something like this
<a href="#" onclick="javascript:location.href='@Url.Action("", "")'" class="RedButton">Add</a>
<a href="#" onclick="javascript:location.href='@Url.Action("", "")'" class="RedButton">Save</a>
<a href="#" onclick="javascript:location.href='@Url.Action("Home", "Index")'" class="RedButton">Cancel</a>
Submitting forms should be done with submit buttons. That's the semantically correct way. That's how forms should be submitted according to the HTML specification.
Anchors should not be used for posting and submitting forms. They are used to redirect and issue GET requests. An anchor cannot submit a form. You will have to write javascript and fake a form submission when it is clicked so that all form fields are sent.
Don't do it, I wouldn't recommend it.
For example things like pressing enter while the focus is inside a textfield will no longer work and you will have to write additional javascript to handle this case to which all internet users are accustomed to. It is a nightmare. The way you did it initially with submit buttons is perfect.
精彩评论