Currently, our application has a lot of ActionLinks that only pass an id to the controller, where the object is retrieved again (that was already present/known on the previous page). Is there any cleaner way to do this, like pass the object instead? As our backend isn't too quick it takes a few seconds to load in the popup which you get after you click on the ActionLink.
I've seen some solutions using ajax to post the object but that doesn't seem like the neatest solution to paste those js lines under/at every ActionLink that is used in combination with a popup.
The #Html.ActionLink() method will generate a url link to the given Controller/Action. Thus, it can only contain parameters that can be contained in the url of the link. So you cannot pass an object through on the url.
If you need to pass through the reference to an object that is stored on the server, then try setting a parameter of the link to give a reference to the object stored on the server, that can then be retrieved by the action (example, the Id of the menuItem in question).
Parameters in the ActionLink are set through the collection that you passed in as the third item in your function call above. Assuming default routing, this would give an address that looks like /Admin/EditPage/?name=XXX where XXX is the value of menuitem.Title. If you included something else here like itemId = menuitem.Id then it would add this as a query string parameter to the url generated, which would then be accessible to the action that is the target of this link.
Related
I want my webpage to be multilingual. That's why I created a Button that changes a session variable to for example fr (French). It's accomplished by an action result that changes the session variable. Now I am not sure what the action result should return so I am going to get to the same page as I was before. Can you help me out?
You can't, not how you describe.
What you can do is set the language as a parameter in your query, either as a regular query parameter (http://example.com/page?lang=kr) or using URL rewriting (http://example.com/kr/page) and use the parameter in the view to render the correct language.
And when you get to writing the language changes links, you use an action link like normal, but you overwrite the language parameter.
Way 1 : Use same concept of return url using query string that used to redirect user back to the same page after login
Way 2 : use ajax request to change language and on success just make reload
Way 3 : add language to url
Actually i found a way to do it myself, if I use this in my ActionResult the page refreshes and my language is changed.
return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);
By using this you just reload the page that was loaded before, so when the variable for the language has been changed by my action controller it pulls the right textes out of my Database and by reloading it uses them.
I'm using the Google ReCaptcha element on a form. ReCaptcha adds a parameter to the POSTed parameters named g-recaptcha-response, which the controller needs to retrieve. Since this parameter name is not a legal C# variable name, it seems that the only way to retrieve its value is via Request.Params["g-recaptcha-response"] (rather than via model binding to a property in the view model).
Now, my problem is that elsewhere in the POSTed parameters I have a couple of form fields that may contain HTML markup. I have annotated the associated properties in my view model with [AllowHtml], which prevents the model binder from throwing an HttpRequestValidationException if the user enters HTML markup into the form. But [AllowHtml] apparently only works in the model binder. If the user has entered HTML markup into the form, then I get an HttpRequestValidationException when I reference Request.Params to fetch the ReCaptcha response.
As near as I can tell, the only way I can fetch the ReCaptcha response while still allowing HTML markup in selected POST parameters is to use go through the pain of writing a custom model binder (e.g. https://stackoverflow.com/a/4316327/1637105) to allow me to bind a property in my view model to an alias (in my case, a property name that is not a valid C# variable name).
The point of this question is just to confirm that I really do need to go to the pain of implementing a custom model binder.
Any suggestions or alternate solutions are more than welcome!
EDIT:
It occurs to me that another solution would be to figure out a way to fetch the value from the POSTed parameters without triggering (or while handling) the HttpRequestValidationException.
You can use the Unvalidated property of Request to access values without triggering Request Validation. for example
var captcha = Request.Unvalidated.Form["g-recaptcha-response"];
Goal:
Display the url address as "http://localhost:49332/Home/Contact2/1?idd=first" (except "http://localhost:49332") instead of "http://localhost:49332/Home/Contact2/1?one=second&two=yes"(except "http://localhost:49332") when you have pressed the button Create
Problem:
I don't know how to do it? I strongly believe that it has to do with querystring.
Info:
*I would like the querystring to take place inside of the method "Public ActionResult contact2(int? id, string one, string two)"
*You can download the source code on website (https://drive.google.com/file/d/0B23pYZkpAyafbEtYTjhYaHhtNkk/view).
I believe that in your case, the easiest way to do this is using the AttributeRouting.
http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx
Or else, you should configure the routes inside RoutConfig class, this link might be helpful https://msdn.microsoft.com/en-us/library/cc668201%28v=vs.140%29.aspx
If you want to use only querystring, just put the action parameter name in the url... everything should work fine.
I'm trying to pass an object through an ActionLink to a method in an MVC controller.
The razor syntax:
#Html.ActionLink("Export to Excel","ReturnExcelOfViewableResponses",new { SearchObject = Model.SearchObject})
What's actually being displayed in markup:
Export to Excel
The controller method is being called just fine, hence no reason to post here. What needs to be done so that the actual values are passed into the actionLink instead of DTO.SearchObject? According to HTML.ActionLink method it looks like I have the right syntax (using MVC 4).
You should be able to pass the DTO, assuming it's just that, as the parameter into ActionLink:
#Html.ActionLink("Export to Excel","ReturnExcelOfViewableResponses",Model.SearchObject)
Any public fields will be added as a query parameter key/value pair.
I am currently using a number of query string parameters to pass some data from one page to a second page (the parameters hold confirmation/error messages to display in the second page), that due to a third party product can no longer work correctly in the production environment. The user completes an action on the first page, and is then transferred to the second page. What are the possible alternatives to use instead of a query string and GET - session variables, POST data, or something completely different?
Thanks, MagicAndi.
You could create public properties in a source page and access the property values in the target page when using a server transfer. You could also get control information in the target page from controls in the source page by referencing the Page.PreviousPage property.
Both of these methods are oulined here: http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
Both POST data and session variables would work just fine. POST data has the drawback that it can be changed by the client and session variables take up memory, so you can choose based on that. I personally don't think that you should pass such messages to the client for the reason stated above but I guess you are already doing that, so...
you can use this if you use window.open("openTheotherPage",...etc)
so form the opened page you can do something like this
var valuefromCallerPage = window.opener.document.FormNmae.textbox.value
or button or anything on the caller page