c# - ActionLink in Table MVC -
edit: have read , implemented method oulined in this article not work.
i have next table populated correctly.
<table class="table"> <tr> <th>product</th> <th>file name</th> <th>release date</th> <th>size</th> <th></th> </tr> @{ if (model != null) { foreach (var item in model.uploads) { <tr> <td>@html.displayfor(modelitem => item.product)</td> <td>@html.displayfor(modelitem => item.displayname)</td> <td>@html.displayfor(modelitem => item.releasedate)</td> <td>@html.displayfor(modelitem => item.size)</td> <td>@html.actionlink("delete file", "upload", "tools", new { id = item.displayname }, null)</td> </tr> } } } </table>
i have controller action
[httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> deleteuser( administratorviewmodel model, string username) { // amazing stuff... homecoming index(); }
i want pass action username selected deletion. thought achive @html.actionlink
above, not way go.
how can pass selected username selected action method?
thanks time.
edit: changing ajax code next (using index
called method)
@ajax.actionlink( "remove", "index", "tools", new { model = item, username = item.username }, new ajaxoptions { insertionmode = insertionmode.replace, httpmethod = "post" })
and changed name of deleteuser(administratorviewmodel model, string username)
method index(administratorviewmodel model, string username)
. fires index
method in toolscontroller
. method called non-post attributed method(!?) have 2 questions:
[httppost]
attribute? why can phone call deleteuser
method using ben griffiths' reply below? how can phone call deleteuser
method in tools controller , pass in model , name of user want delete? thanks time.
the 4th argument overload of actionlink extension method using when call
html.actionlink("delete file", "upload", "tools", new { id = item.displayname }, null)
is route values
parameter, can utilize pass info controller.
currently, in above call, upload
action method on tools
controller receiving id
argument. if want pass display name deleteuser action method, use
html.actionlink("delete user", "deleteuser", "[controllername]", new { username = item.displayname }, null)
however, deleteuser
method decorated httppost
attribute, meaning action take requests utilize post method. means have 3 options:
(1) remove [httppost] attribute action method - not idea, since imagine don't want exposing action requests reason.
(2) utilize form containing submit input , hidden displayname input instead of link.
(3) utilize ajaxhelper.actionlink extension method create asynchronous postback controller. e.g.:
ajax.actionlink("delete user", "deleteuser", "[controllername]", new { username = item.displayname }, new ajaxoptions{ httpmethod = "post" })
update: more finish illustration of 3rd option here's working (albeit simple) illustration of 3rd option. i'm not 100% sure ultimate goal is, rather seek provide realistic illustration i've tried create 1 simple clear. note i've omitted handling of anti forgery token aid in clarity, i've stuck async
action method not deviate far real life.
controller:
public class homecontroller : controller { private async task<string> delete(string displayname) { thread.sleep(1000); homecoming string.format("{0} has been deleted", displayname); } [httppost] [allowanonymous] public async task<jsonresult> deleteitem(string displayname, int product) { task<string> deletetask = delete(displayname); homecoming new jsonresult() { info = new { product = product, result = await deletetask } }; } public actionresult index() { administratorviewmodel model = new administratorviewmodel() { uploads = new list<itemmodel>() { new itemmodel() { displayname = "first one", product = 1, releasedate = datetime.now, size = 11 }, new itemmodel() { displayname = "second one", product = 2, releasedate = datetime.now.adddays(1), size = 12 } } }; homecoming view(model); } }
model:
public class administratorviewmodel { public ienumerable<itemmodel> uploads { get; set; } }
layout:
<!doctype html> <html> <head> <title>demo</title> </head> <body> @renderbody() <script src="~/scripts/jquery-1.10.2.js"></script> <script src="~/scripts/jquery.unobtrusive-ajax.js"></script> @rendersection("scripts", false) </body> </html>
home/index view:
@model administratorviewmodel <table class="table"> <tr> <th>product</th> <th>file name</th> <th>release date</th> <th>size</th> <th></th> </tr> @{ if (model != null) { foreach (var item in model.uploads) { <tr> <td>@html.displayfor(modelitem => item.product)</td> <td>@html.displayfor(modelitem => item.displayname)</td> <td>@html.displayfor(modelitem => item.releasedate)</td> <td>@html.displayfor(modelitem => item.size)</td> <td>@ajax.actionlink("remove", "deleteitem", "home", new { displayname = item.displayname, product = item.product }, new ajaxoptions { httpmethod = "post", oncomplete = "itemdeleted" }, new { id = item.product })</td> </tr> } } } </table> @section scripts { <script> var itemdeleted = function (data) { var $link = $('#' + data.responsejson.product); $link.parents('tr') .children() .css('text-decoration', 'line-through'); $link.remove(); alert(data.responsejson.result); }; </script> }
c# asp.net html5 razor asp.net-mvc-5
No comments:
Post a Comment