What is the analog in Razor notation? - c#

So, in example of DotNetOpenAuth I have form in aspx:
<form action="Authenticate?ReturnUrl=<%=HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]) %>" method="post" id="openid_form" %>
</form>
And what will be analog of it aspx in Razor?
#using (Html.BeginForm(---???---)) {}
--- update---
Thanks for all for suggestions, answer is:
#using (Html.BeginForm("Authenticate", "Account", FormMethod.Post,
new { target = "_top", id = "openid_form" })){}

You don't need to call BeginForm; you can still write <form> tags in Razor.
BeginForm is used when posting to MVC routes.
If that is an MVC action, you can write
#using(Html.BeginForm("Authenticate", new { ReturnUrl = Request.QueryString["ReturnUrl"] }))

<form action="Authenticate?ReturnUrl=#Request.QueryString["ReturnUrl"]" method="post" id="openid_form" %>
</form>
There are multiple ways of doing it, though this is the cleanest way that most closely resembles the sample code you posted.

Related

ASP.Net Core | Issue with Html.ActionLink

I'm new to ASP.net core and I was trying to write a redirect action using #Html.ActionLink
Here is my code :
#page
#model IndexModel
#{
ViewData["Title"] = "Home page";
}
<div class="text-center">
<h2>Main Navigation</h2>
<ul>
<li>#Html.ActionLink("PRIVACY PAGE", "Privacy", "Privacy")</li>
</ul>
</div>
This action link is supposed to redirect to the PrivacyPage, but instead, the values that I pass are being sent as a parameter to the URL
https://localhost:7020/?action=Privacy&controller=Privacy
The expected result is
https://localhost:7020/Privacy
Not sure what I'm missing. Will this ActionLink work only for the MVC projects?
Thank You !!
If i understand you correctly, I think you put wrong controller name.
#Html.ActionLink("link text", "actionname", "controllername")
Instead of #Html.ActionLink("PRIVACY PAGE", "Privacy", "Privacy") it will be #Html.ActionLink("PRIVACY PAGE", "Privacy", "Home")
You can use asp-page attribute with Razor Pages.
<a asp-page="/Privacy">Privacy</a>
if your Privacy page in another folder like /Pages/Home/Privacy
<a asp-page="/Home/Privacy">Privacy</a>
For more documentation https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/anchor-tag-helper?view=aspnetcore-6.0

PageData fails to pass data in WebMatrix

I am building a website with WebMatrix. I would like users to enter their name in the main page and after redirection their name will be shown in the results of another form. But my code is not working.
This is a snippet of the main page:
#{
if (IsPost) {
PageData["fullname"] = String.Format("{0} {1}", Request.Form["mainForename"], Request.Form["mainSurname"]);
PageData["redir"] = Request.Form["goTo"];
}
}
<form name="mainForm" id="mainForm" method="post" action="foo.cshtml" onsubmit="return mainValid(this);">
<h2>Please enter your name:</h2>
<label for="mainForename" class="label">Forename:</label>
<input type="text" name="mainForename" id="mainForename">
<label for="mainSurname" class="label">Surname:</label>
<input type="text" name="mainSurname" id="mainSurname">
<input type="submit" name="goTo" value="Go to Form 1">
<input type="submit" name="goTo" value="Go to Form 2">
</form>
This is a snippet of the page that the main page directs to:
#{
if (IsPost) {
var display = PageData["fullname"];
}
}
<form name="form1" id="form1" method="post" onsubmit="return Valid(this);">
<!-- some HTML code -->
<input type="submit" name="submit" value="Get results">
<p>#Html.Raw(display)</p>
</form>
But whatever value I have submitted in the mainForm, PageData["fullname"] and PageData["redir"] seem to have no values. What is the problem?
Any help would be appreciated.
I think PageData is only useful when combining subpages into a single page.
Instead, try the Session object where you are using PageData. Session will be available for all that user's pages.
So where you have PageData["fullname"] use Session["fullname"]
For more details, see http://www.mikesdotnetting.com/article/192/transferring-data-between-asp-net-web-pages
I find something that's not quite good in your code:
Why your form action is set to a cshtml file? It has to be an action in a controler;
Why are you using "hardcoded" form tag? Use #using(#Html.BeginForm('name', 'action', 'controller'...)
Why do you need WebMatrix? 1st - its pretty old, 2nd it for grids - you have a form.
Use a model, and use #Html.TextBoxFor(x=>x.UserName) inside the #Html.BeginForm.
Then post the form in the action you are posting to redirect to another page that contains the 2nd Form, and have a model. The post action should look somehow like this
[HttpPost]
public ActionResult RedirectToAnotherForm(MyModel model)
{
return View('SecondFormView', new SecondFormModel{
userName = model.name
})
}

ASP MVC - Html.BeginForm from within an area for a controller without area

I have a view that is created within a MVC area.
from this view i want to add an Html form that gets to an action on a controller that is not in any Area but just in my main controller folder.
#using (Html.BeginForm("MyAction", "MyController", FormMethod.Get))
{
<input type="submit" value="TEST" />
}
When i execute this code it tries to find my controller in the same area and fails, following html is generated
<form action="/MyApp/en/MyController/MyMethod" method="get">
this actually works on other calls:
http://localhost:18183/MyApp/nl/OtherController/OtherAction
My action on the controller without area can be reached in the browser like this:
http://localhost:18183/MyController/MyMethod
But when i add the form the classic way it still doesn't work:
<form action="/MyController/MyMethod" method="get">
How can i achieve to call this controller?
You need to specify an annonymous object to the routeValues attribute:
#using (Html.BeginForm("MyAction", "MyController", new { area = "" }, FormMethod.Get))
{
}
If you wanted to go to a different area rather than no area, modify the empty quotes to be your new area
Documentation for this overload is available at http://msdn.microsoft.com/en-us/library/dd492933(v=vs.118).aspx

MVC Html.ActionLink with post functionality?

I'm checking to see if anyone has written an MVC extension for Html.ActionLink that you can pass in Post parameters like such:
<% Html.ActionLink("Click me", "Index", "Home", new { MyRouteValue = "123" }, null, new { postParam1 = "a", postParam2 = "b" }); %>
That would render the link like normal but having an onClick event that submits an also rendered form with an Action url for the Action, Controller, and Route Values with additional hidden inputs from the Post Parameters like such:
Click me
<form id="theform" action="/Home/Index/123" method="post">
<input type="hidden" name="postParam1" value="a">
<input type="hidden" name="postParam2" value="b">
</form>
I'm looking to redirect users to various pages with potentially a lot of data. Not only from page to page, but from email to page also. This would be highly reusable and I think would clean up a lot of code, and would save a bunch of time writing this if its already floating around out there. I hate recreating the wheel when I don't have to.
ActionLink is just for creating an <a>. What you are asking for would blow up if it is already inside of a form. If it isn't then it is preferable to make the link the submit button inside the form and NOT use javascript (javascript and emails don't get along great).
You could create the form and appende it to the end of the DOM. This could be done through partial view or through javascript.
Honestly I suggest you don't use a POST. If you persist most of the data and just have the ids needed to retrieve said data, you should never have to pass too much data in an actionlink.
Ajax.ActionLink works perfectly fine for a post request. To refresh page, you can create a function that refreshes page (e.g. function reload(){ windows.location.reload();}).
It would look something like this.
#Ajax.ActionLink("DiaplyName", "Action", new { parameters to post }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, OnComplete="reload();"})
Note: You'll need to reference the appropriate scripts to use ajax or jQuery code.
This piece of code was helpful for me and saved my day.. I improved it and it helped me for Impersonated user.. here is bellow ,what i did..
<% if (Session["SessionUserImpersonate"] != null && Session["SessionUserImpersonate"] != "" && Session["SessionUserImpersonate"] == "Yes")
{
BLL.Models.User userold = new BLL.Models.User();
userold = (BLL.Models.User)Session["SessionUserOld"];
%>
<span class="FL">(Impersonated as <%=Website.Backoffice.SessionHelper.Session_User.UserName != null ? Website.Backoffice.SessionHelper.Session_User.UserName:"" %> , </span>
<form class="FL" id='frmid' action="/Index/Login?username=<%=userold.UserName%>&password=<%=userold.Password%>&IsImpersonate=No" method="post">
<a class="TxtRed" style="cursor:pointer;" onclick="$('#frmid').submit(); return false;" > - finish impersonated session </a>
</form>
)
<%} %>

Aliasing Querystring Params in BeginForm with FormMethod.Get - MVC2

The Question
Is there a way to alias the parameters generated by MVC, in my case it would be the searchTerm param? For example, in my viewmodel it would still be SearchViewModel.searchTerm but the URL would look more like /Home/Search?s=jimmy
Search Form
<% using (Html.BeginForm("Search", "Home", FormMethod.Get)) {%>
<%: Html.TextBoxFor(model => model.searchTerm) %>
<input type="submit" value="Search" />
<% } %>
View Model
public class SearchViewModel
{
public string searchTerm { get; set; }
}
The URL Generated
/Home/Search?searchTerm=jimmy
Edit: Doesn't seem possible. Looks like I'll have to do it on my own..
I think you could either change your textbox control to not use templating and use the name your want to see in the query string like this...
<%= Html.TextBox("s", Model.searchTerm)
Or you could change the template that the TextBoxFor method uses with a own custom one, but I'm not sure what convention you would use. Maybe check for modelnames of "searchTerm"?
This site has some examples of how to create a custom template.
Sorry, if the syntax was not correct in the code, but I do not have MVC on this machine.

Categories