encrypt parameters in #Html.ActionLink - asp.net-mvc

i am trying to encrypt my url id from asp.net razor
In my view I have the following #Html.ActionLink:
#Html.ActionLink(" ","index", "Home", new { id = Server.UrlEncode(item.ID.ToString()) }, new { #class = "fas fa-eye", #title = "Ver Detalle" })
With that I get the link as follows:
http://localhost:62201/Home/index/1
And the idea is to hide or encrypt the id for "more security", something like this:
http://localhost:62201/Home/index/unrecognizable_id
And then this is decoded in my controller
I appreciate your help in advance.

If you want to hide the Id,you can try to use form post:
#using (Html.BeginForm("index", "Home", FormMethod.Post))
{
<input hidden name="Id" value=#item.ID />
<input type="submit" value="submit" />
}
result:
In addition,If you want to use encrypt,you can refer to the link.

Related

Correct way to handle multilple buttons on Begin Form

Would like to ask whether this way is correct or not to call two actions in a begin form
This works fine for me,would like to know whether this is the correct way or not.
I have added formaction="/Controller/Action" in button.
View
#using (Html.BeginForm("FileUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="submit" value="Update" id="btnSubmit" />
<input type="submit" value="Reset" formaction="/Home/Reset" id="btnReset"/>
}

MVC ASP.NET Map routing is not working with form GET request

In View-
#using (Html.BeginForm("PageName","ControllerName", FormMethod.Get))
{
<input type="hidden" name="categoryName" value="Insurance" />
<input type="hidden" id="cityName6" value="Irvine" name="cityName" />
<input type="hidden" name="page" value="1" />
<input type="submit" class="btn btn-default" value="Insurance" />
}
In RouteConfig-
routes.MapRoute(
"SomethingRouteName",
"{categoryName}/{cityName}/{page}",
new { controller = "ControllerName", action = "PageName" }
);
I want url to appear like this - Insurance/Irvine/1
But its coming like this- ControllerName/PageName?categoryName=Insurance&cityName=Irvine&page=1
This works fine when I use hyperlink instead of form get method.
#Html.ActionLink("Insurance", "PageName", "ControllerName", new{ categoryName = "Insurance", cityName = "Irvine", page = 1})
//URL shown: Insurance/Irvine/1 as expected.
But I have to use form GET method so this hyperlink way is useless.
Please help
You're not passing any route values to Html.BeginForm, so your rendered form element looks like this:
<form action="/ControllerName/PageName" method="get">
</form>
So when you click submit, it simply appends the values of the form as a query string.
To fix this:
#using (Html.BeginForm("PageName", "Home", new {categoryName = "Insurance", cityName = "Irvine", page = "1"}, FormMethod.Get))
{
<input type="submit" class="btn btn-default" value="Insurance" />
}

asp.net mvc #Html.BeginForm diplays unwanted text

I am developing a web application without any models. I just do not need them for what I am trying to do. I want to take some input from the view and to pass it on the controller. So far so good. The problem is that on the view I get some unwanted text: "System.Web.Mvc.Html.MvcForm"; it comes from the follwoing line: #Html.BeginForm("GetInfo", "Test", FormMethod.Post, new { id = "TestForm" } )
More precisely, my code is this:
--the view
#Html.BeginForm("GetInfo", "Test", FormMethod.Post, new { id = "TestForm" } )
<br/>
<legend>Report</legend>
<br />
<div class="editor-label">
<label>Date From</label>
</div>
<div class="editor-field">
<input name="dateFrom" type="date" required />
</div>
<br />
<input type="submit" class="btn btn-primary" />
and the method from the controller is :
public ActionResult GetInfo(DateTime dateFrom)
{
//some code
return RedirectToAction("Index");
}
Any ideas?
Thank you in advance! :)
Try
#using (Html.BeginForm(.....))
{
// Html here
}

T4MVC and Ajax method with parameter

I am trying to apply T4MVC to my project. Say, I have an ajax search box, it calls Home/SearchQuery action which takes in a string q as parameter. How do I write that line in T4MVC?
From Ajax.BeginForm("SearchQuery", "Home", ....
To Ajax.BeginForm(MVC.Home.SearchQuery(???)...
.cshtml file
#using (Ajax.BeginForm("SearchQuery", "Home", /* <-----Convert to T4MVC Here */
new AjaxOptions {
LoadingElementId = "loadingGif",
OnSuccess = "parseResults",
OnFailure = "searchFailed"
})) {
<input type="text" name="q" />
<input type="submit" value="Search" />
<img id="loadingGif" style="display:none" src="#Url.Content("~/content/images/loading.gif")" />
}
<div id="searchResults" style="display: table"></div>
Your q is submitted from the input in form, so you could just write
#using (Ajax.BeginForm(MVC.Home.SearchQuery(),
new AjaxOptions {
LoadingElementId = "loadingGif",
OnSuccess = "parseResults",
OnFailure = "searchFailed"
})) {
<input type="text" name="q" />
<input type="submit" value="Search" />
<img id="loadingGif" style="display:none" src="#Url.Content("~/content/images/loading.gif")" />
}
Another possible answer: regenerate the template
I know it's a bit stupid, but I got here just because I forgot to regenerate the classes with the template (the new method with parameters is accessible before you regenerate the templates). Maybe someone will find this usefull.

HTML.ActionLink vs Url.Action in ASP.NET Razor

Is there any difference between HTML.ActionLink vs Url.Action or they are just two ways of doing the same thing?
When should I prefer one over the other?
Yes, there is a difference. Html.ActionLink generates an tag whereas Url.Action returns only an url.
For example:
#Html.ActionLink("link text", "someaction", "somecontroller", new { id = "123" }, null)
generates:
link text
and Url.Action("someaction", "somecontroller", new { id = "123" }) generates:
/somecontroller/someaction/123
There is also Html.Action which executes a child controller action.
Html.ActionLink generates an tag automatically.
Url.Action generates only an url.
For example:
#Html.ActionLink("link text", "actionName", "controllerName", new { id = "<id>" }, null)
generates:
link text
and
#Url.Action("actionName", "controllerName", new { id = "<id>" })
generates:
/controllerName/actionName/<id>
Best plus point which I like is using Url.Action(...)
You are creating anchor tag by your own where you can set your own linked text easily even with some other html tag.
<a href="#Url.Action("actionName", "controllerName", new { id = "<id>" })">
<img src="<ImageUrl>" style"width:<somewidth>;height:<someheight> />
#Html.DisplayFor(model => model.<SomeModelField>)
</a>
#HTML.ActionLink generates a HTML anchor tag. While #Url.Action generates a URL for you. You can easily understand it by;
// 1. Item Definition
#HTML.ActionLink("Item Definition", "ActionMethod", "ControllerName")
// 2. /ControllerName/ActionMethod
#Url.Action("ActionMethod", "ControllerName")
// 3. Item Definition
Item Definition
Both of these approaches are different and it totally depends upon your need.
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm("Index", "Company", FormMethod.Get))
{
<p>
Find by Name: #Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
<input type="button" value="Clear" onclick="location.href='#Url.Action("Index","Company")'"/>
</p>
}
In the above example you can see that If I specifically need a button to do some action, I have to do it with #Url.Action whereas if I just want a link I will use #Html.ActionLink.
The point is when you have to use some element(HTML) with action url is used.
You can easily present Html.ActionLink as a button by using the appropriate CSS style.
For example:
#Html.ActionLink("Save", "ActionMethod", "Controller", new { #class = "btn btn-primary" })
I used the code below to create a Button and it worked for me.
<input type="button" value="PDF" onclick="location.href='#Url.Action("Export","tblOrder")'"/>

Resources