Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
first I'm using mvc2.0
i don't want to use

<br />
<![CDATA[<% using (Html.BeginForm()) { %>]]><br />

rather I want to use <form> tag
and want to set its action as /Details/Delete/id

id will be the id of that record whose Delete Button is clicked


how can I control this behaviour
Posted
Updated 19-May-10 6:01am
v4

Hi,

MVC is designed to post the model for the page back to server side on postback. This means that the action of the form needs to be controlled by the framework.

You can create a view on a different page to handle the delete and call the page using ajax.

There following is a url with a simple introduction to ajax.

http://devzone.skillfusion.com/ajaxArticle4.php
 
Share this answer
 
VB
The "HTML." methods in MVC are convienience methods that generate HTML code so you can always override them with your own HTML.


The limited amount of details make it difficult for me to understand why you would need to override the form post action.

Based on your question it looks like you may have a grid with multiple delete buttons. You can add an onclick event to your delete button that would look something like this:

onclick='window.location="/Details/Delete/<%= item.id %>"; return false;'

"item.id" would be the ID of the item being displayed on the grid line or any ID you want to pass back.

If you really want to alter the action and post the form you can use the following:

onclick='document.forms[0].action="/Details/Delete/<%= item.id %>"; document.forms[0].submit(); return false;'
 
Share this answer
 
v2
Hi

the <% using (Html.BeginForm()) { %> has an overload method Html.BeginForm(string actionname, string controllerName, RouteValueDictionary routeValues).

So this code...
<br />
<% using (Html.BeginForm("myTestmethod", "myTestController", new { id = "123" }))<br />
       { %><br />

will generate this html markup....

<br />
<br />
<form action="/myTestController/myTestmethod/123" method="post"><br />
<br />


as you have asked passing the id. However you need to have a matching route for this..

so in your routes ..define any specific routes as well...


C#
routes.MapRoute(
               "Default", // Route name
               "{controller}/{action}/{id}", // URL with parameters
               new { controller = "myTestController", action = "myTestmethod",  id = UrlParameter.Optional } // Parameter defaults
           );


I hope this helps you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900