I have a page with for example personal data, I want to have a Go button for other items such as job data but on the same current page with a Go button (next) and so on...
What is this characteristic called Please provide a link to it, or a brief explanation. (like multiview in old version 'asp.net web form')
I'm using asp.net core
This problem could be solve by using ajax.
In your controller you have to make a action just like this
public JsonResult GetYourData()
{
//Add your data here. Make a string along with your data like '<h2>Job Data</h2><p>Title:Software-Developer</p>'
return Json(yourCreatedString, JsonRequestBehavior.AllowGet);
}
Now in view you could add an event on the button click, ask for this above action, get the html in string format, and append that to your DOM
Related
I am calling controller action method from dialog box from view. I want to return to same dialog box in same view. what return type should I specify in controller action method.
I suggest you take a look at Progressive enhancement tutorial with ASP.NET MVC 3 and jQuery. It provides a pretty simple example on how to update a dialog box with new content by adding jQuery to perform the requests needed for updating the UI.
Hope it helps!
So my problem is that i want to hang an event to a checkbox in the view like this:
as you can see i ask for a notification in the changeFilter() method as soon as the checkbox changes. However when i create that method in the controller like this:
public EventHandler changeFilter()
{
ViewBag.test = "test";
return null;(ignore the null please i know it's bad)
}
it doesn't give me any results sadly nor does it ever reach that code. i want to reach that code so i can put filters on a list runtime that it refreshes and modifies the search result.
Does anybody have experience with this or anybody knows anything about it? i'd love some help
Thanks a lot in advance,
Marijn
If you are coming from the ASP.Net Webforms, it will not work as you are requesting. you need to change little bit when you are using the MVC. Here are the steps you need to follow,
Please attach javascript method on click of your check box.
in javascript method, make a ajax call to your controller method.
In Ajax success handler, parse the json which is returned from the controller method.
Good luck...
First, some context:
In my controllers, I return RedirectToAction's on successful HTTP POSTs. I use TempData to hold onto the user's entered model data so that the method I redirect to can use this input data again.
Example:
1. Enter userID into search field.
2. Click button, POST is performed, user is found in database through my repository, userID stored in TempData, call RedirectToAction("Edit")
TempData["user"] = searchViewModel.userID;
return RedirectToAction("Edit");
perform edits on Edit view, click commit button, user info is stored in TempData, call RedirectToAction("Confirm");
display changes made on the Confirm view, click "Confirm", final HTTP POST is performed and changes are made through my repository service.
This seems to work well. In order to handle people trying to skip ahead to a page in the address bar by typing "../Edit/Confirm" I have this check in my Confirm method:
if (TempData["editUserViewModel"] == null)
return RedirectToActionPermanent("Edit");
Is this the best way to handle address bar input? I also do TempData.Keep("editUserViewModel") so that refreshes work. Is this the best way to handle refreshes?
For going from step 1 to 2, I would suggest a paramaterized action instead:
Enter userID into search field.
Click button, POST is performed, user is found in database through my repository
Call RedirectToAction("Edit", new {UserId = foundUserId})
Also, when searching, you probably shouldn't be doing a POST. A GET is just fine when you are looking for information and not mutating it. This avoids the PRG pattern altogether for the first place where you are using tempdata, since you do a GET instead of a POST.
As for the confirm, there is another way to do this without tempdata. Instead of redirecting to your Confirm action, POST to it, and return your confirm viewmodel. Only after that second POST do you hit the repos and finish out the PRG pattern after the POST with a Redirect and finally a Get.
Users should not be able to do any type of GET for your Confirm action, as can be seen by your bandaid for it. So, just don't allow gets at all. POST from the edit form to the confirm action, return a view, and then POST from that view to a second POST action method. Since these are all part of the same process, you shouldn't have to deal with redirects or tempdata until the process is complete (repos updated).
Update (reply to comments)
1.) If I remove the [HttpPost] attribute on my SearchUser function, how will my search button on the view know what to call?
Your search button is nested within a <form> HTML element. You will need to change the form's method to GET. If the attribute is not present, I believe POST is the default. Your search button will remain the same, but the form will submit the user-enetered input as an HTTP GET request instead of an HTTP POST:
<form method="GET">
...
<input type="submit" value="Search" />
</form>
2.) Can you clarify removing the Redirect to Confirm? I'm having trouble understanding how I would change a Redirect to a POST
It's difficult to explain this to someone just starting with web development, but in essence, every redirect is always an HTTP GET request. This is why you had to put the data into session (tempdata uses session) in order to maintain it across stateless requests.
Basically, here is your workflow:
User enters search input and clicks submit
The search in (1) is sent as a GET request to some action method, which returns a view.
The view returned in (2) contains a <form method="POST" action="/Users/StillNeedsConfirmationAction"> with additional input elements. This is the form that will be used to edit data.
User inputs data in the form view from (3) and clicks submit.
The action method StillNeedsConfirmationAction on your UsersController accepts an HTTP POST with a viewmodel object. Rather than redirecting though, the action simply returns another view, passing the same viewmodel object.
The view returned in (5) contains a <form method="POST" action="/Users/ConfirmAndUpdateAction">. It will render hidden inputs for each text box or other form element in your previous POST form.
User clicks submit on the second form to confirm fields
The action method ConfirmAndUpdateAction on your UsersController accepts an HTTP POST with the same viewmodel object that your other POST action did. However instead of returning another view, this time it saves the data in your repository and redirects.
Problem
I have some pages that need dynamic data from website to generate output to users.
A simple solution is an aspx(php, ...) page to generate data and create another html page serving as GUI retrieving data from first page and showing it to users. in this method I can call my GUI page for example form1.aspx and my data page form1.json.aspx.
although I personally like this method, it is not suitable when creating components for it.
Another method that currently I'm using is using same GUI page call itself with a querystring to retrieve data. this page should check for that query string and if it exists, only generate data and remove everything else from page. As an example for this method if I call my page form1.aspx, to retrieve data, I need to call it like form1.aspx?JSON
Here is an example of what I'm doing:
protected void Page_Load(object sender, EventArgs e) {
if (Request.QueryString.ToString().IndexOf("JSON") == 0){
this.Controls.Clear();
Response.Clear();
// send pure data to client
} else {
// render page as GUI
}
}
However this method becomes too messy if I add master page and/or inherit my page from some template page. Master pages can only removed in Page_PreInit and that adds another extra method.
Security controls cause another problem, if user leaves page open for long time until session expires any attempt to retrieve data will fail cause security module will redirect the request to login page.
Next problem is I cannot consolidate my component in package because it needs modification in page (removing master page, clearing page components ...).
What I'm looking for:
1- I'm looking for a solution that I can call my page and get pure data (JSON or XML format) and doing so run a server side method that generates data, so I don't have to worry about what another designer puts in their master page or template.
2- I think it is possible to use axd extension to do this but I don't have a clue about it and couldn't find a helping document either.
3- Is there any better way of doing this. any suggestion or solution to improve this much appreciated.
Page methods. Check this article: http://weblogs.asp.net/craigshoemaker/archive/2008/09/29/using-jquery-to-call-asp-net-ajax-page-methods.aspx or http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
WCF JSON service: http://www.codeproject.com/Articles/327420/WCF-REST-Service-with-JSON
Other ways of doing is using an HTTP Handler. Implement IHttpHandler interface and register your implementation in your Web.config file. Later call it using jQuery ($.get / $.post):
http://msdn.microsoft.com/en-us/library/46c5ddfy.aspx
EDIT
As OP pointed out, in order to access session state in a page method you should use WebMethodAttribute this way:
[WebMethod(EnableSession = true)]
I think you can use webservice instead of aspx page to return a JSON or XML string and then the caller page (any aspx page) will response after process is success.
So with this webservice, any third party page will have access to your server side method.
To create a webservice pls Check this link: Create and use Asp.net web service basic
Regards
I'm developing a new website for my employer which contains a lot of CMS-style features. One of them is being able to create pages, specify URLs, menus, etc.
This is all fine.
What I'm after is a way to create a "Preview" button on these pages, that does not write to the database. I want to post (preferably the entire Model, if not, just the form data) to a new window and have that view render the page.
I have searched high and low and I cannot find an example that makes sense. Most people seem to have given up :/
I've attempted this by myself with TempData, however TempData gets cleared before I can render the page (as there is more than 2 steps involved from what I can deduce..).
I must POST as the form data will contain HTML (inside a Telerik Editor control).
Does anyone have any idea on a nice way to accomplish this? Or can anyone provide links to some resources? (I've come up completely blank!)
Regards,
chem
You can store the model in Session State.
I would store the data in session state.
this may be useful:
http://davidhayden.com/blog/dave/archive/2011/02/09/SessionLessControllersMvc3.aspx
You can still use TempData. At first step you need explicitly tell leave value in Session, for example:
string messageValue = (string) TempData.Peek("message"); // Does not cause ejection
You could serialize the model to XML and then the action method for the preview page could deserialize it.
See this link.
Thanks for the help guys/gals.
I managed to use TempData.
I basically ajax post to an action that stores the content in TempData using a Guid as the key. The Guid is returned and once the ajax post returns a hidden form with a target="_blank" action posts to a Preview action method passing through the Guid. That preview action then renders the view with the content in TempData.
Thanks for the suggestions.. it got me thinking!
Regards,
chem