Returning A JSON View In Combination With A Boolean?
Apr 13, 2010
What i would like to accomplish is that a partiel view contains a form. This form is posted using JQuery $.post. After a successfull post javascript picks up the result and uses JQuery's html() method to fill a container with the result.
However now I don't want to return the Partial View, but a JSON object containing that partial view and some other object (Success -> bool in this case).
I tried it with the following code:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Item item)
{
if (ModelState.IsValid)
{
try
{
[Code].....
However I don't get the HTML in this JSON object and can't use html() to show the result. I tried using this method to render the partial as Html and send that. However this fails on the RenderControl(tw) method with a: The method or operation is not implemented.
// // GET: /TaxStatements/CalculateTax/{prettyId} public ActionResult CalculateTax(int prettyId) { if (prettyId == 0) return Json(true, JsonRequestBehavior.AllowGet); TaxStatement selected = _repository.Load(prettyId); return Json(selected.calculateTax, JsonRequestBehavior.AllowGet); // calculateTax is of type bool }
I had problems with this because when using it in jquery functions I had all sorts of error, mostly toLowerCase() function failing.
So I had to change the actions in a way that they return bool as string (calling ToString() on bool values), so that thay return "true" or "false" (in the qoutes) but I kinda don't like.
I was wondering to use JQuery with the light wieghted Json and it was a wonderful experience and a very positive performance boosting or the entire project.
I was hoping that anyone else had the same experience with asp.net project geared up with Jquery and Json?
I would like to return an function in some JSON. How can I stop my function being output as text (how can i stop it being wrapped in quotation marks?).
eg: [Code]....
In script the value of name is just a string. Is there some object I can set name to that will wrap my script and emit as script not a string?I want to do this to better control the "remote" rule of jQuery validate. I have my own dataannotationmodelvalidator for it and its doing this atm:
[Code]....
Which is working fine for the most part except I can't get the value of 'name' to be a function. It comes out as a string. Alternatively is there someway to control what JSON serializer this thing is using alternatively. We need a wrapper function so you can do something like:
name = Emit("function(){ return $(this).val(); }")
This is probably something simple. The following JQuery call is executing and calling the controller. The controller is executing correctly. I used the sample data that's commented out to verify that the templates were working also. However, the success function is not executing in the browser. Any thoughts or other ways I can debug this?
I have been trying for two months to get this code working, and I am close, but still confused. I want the JQuery UI Autocomplete function to call an web service which returns JSON data and display that data for selection, and on selection put the selected value into a hidden field.
There are several issues:
1) The autocomplete function is not firing 2) The source: "/AutoSuggest.asmx/DOTFind?" line throws an invalid object exception 3) The service requires two parameters: (string prefixText, int count) - count tells it how many records to return. 4) I am not at all certain that this code will accept JSON data that comes back from the service
How can i use a checkbox to represent a boolean bit field in an MVC View? Out of the box, it defaults to a textbox..There is no boolean datatype in SQL Server 2008 so i used a bit datatype. I guess its a custom template but i cant seem to get the syntax right.
I'm having problems passing values from the controller to the view. I created a boolean variable to use as a flag and want the view to render some html based on whether its true or false.
Unfortunately ViewData doesn't look like it can pass boolean values. I should be able to pass any datatype to a view (string, int, bool, etc...)
I Need by service contract to return the xml/json result depending on the request type. function which will convert my result set (i am using linq to sql) so that i do not need to create the xml format for the result set by iterating through the table row many times.What is the suitable way to do that.
I need a kind of short cut method which will convert the table data to xml result.Had i been using asp.net mvc i would have been able to generate the xml data by overriding the the ExecuteResult method in the ActionResult and giving Conetnt-Type = "text/xml" as OP.But since i am using Wcf i don't have the controller context(as controller context is the parameter that needs to be passed to Execute Result).
My present code for converting the table data to the xml format is below.
public XDocument UsersLists(string authToken) { bool IsAuthenticated = Authenticate(authToken); XDocument xDoc = new XDocument(); XElement root = new XElement("Users");
[Code]....
I need to eliminate this way of generating xml for each table records.
created a create view for an entity in my Linq model which had a bit field in it but it turned out a textfield..can you give me an example of an ascx that will enable me use a checkbox for this instead of a textbox, which the create view template uses for ALL the fields.Also, how can i get the template to use html.displayfor() instead of html.textbox?
I know that variations of this same question have been posed numerous times - but I can't quite seem to find the exact solution.
I would like any row in my gridview where the boolean field "Approved" is True to be shaded gray. Here is the code that I'm using, but every row ends up being gray, regardless of the value in that "Approved" field. My guess is that I'm not correctly evaluating the boolean value in this field (which is actually a yes/no field in MS Access), Not sure if I should be reading that value as "-1", "Yes" or "True" or "NotNull"? Anyway, here is the code:
I've written a View (form) that has a section with two radio buttons and a blank <DIV> block that will dynamically be updated. When one radio button is clicked, a <DIV> on the page is updated with fields specific to that radio button option. When the other radio button is clicked, the <DIV> on the page is updated with fields specific to that radio button option. To determine what content gets displayed when a radio option is selected, I call a JavaScript function and set the content appropriately using jQuery. Pretty easy so far. Let's assume that the User has toggled the radio buttons and filled in some data. Further down in the form, the User has entered an invalid e-mail address. When the User submits the View, the ModelState is invalid. If it is invalid, I return the View.
[Code]....
Problem is, when the View is returned, some of the form fields (that are static) are in place. However, the <DIV> that was previously updated based on User radio button selections is reset to its original state. When we return a View because of an invalid Model State, is MVC smart enough to return the View in it's modified state (in this case, with the modified DIV and dynamically added content)?
I have a partial view (an ascx page (see below)) that is used on every page on my site. Its basically a form, when the user hits submit I need it to call a procedure in one of my controllers and then return the page the user was on.
[Code]....
Public Function TalkToUs(ByVal customer As Customers) As PartialViewResult
Once the action filter handles the validation, instead of redirecting to another pages is it possible just to return the view associated with the action with the updated modelstate? Basically this is to handle business validation exceptions thrown by the business layer in a generic way.
I'm building an ASP.NET MVC site where I want one of the Views I return to be automatically scroll to a certain point.The part of the site where I want this to occur works sort of like a forum - there are "threads" that contain "posts". A user can either browse to the whole paginated thread or can browse to a specific post, using its ID. When a user browses to a specific post, I want to show the regular thread interface, then browse to the page that the post is on and scroll down to the post.
Is it possible to somehow automatically scroll down to a certain point when returning a View from an action in an ASP.NET MVC site? If so, how do I do this?NOTE: One solution to this problem that I've found is how Stack Overflow and the other Stack Exchange sites do it: each answer to a question can be linked to by adding #ID to the URL. If it's impossible to automatically scroll down when returning a View, I would implement this instead, but I don't understand how to use such an approach when there are multiple pages and the post in question isn't on the current page.UPDATE:Based on Chris' answer, I'm currently planning to implement it with the URL looking like this: example.com/forum/[ForumID]/thread/[ThreadID]/post/[PostID]#[PostID]. In my Action, I figure out what page of the Thread the Post is on, and then I return all the Posts from that page to the View.However, I noticed something special in how Stack Overflow solves this problem. Try going to: http://meta.stackoverflow.com/questions/57170 - it ends up sending you to http://meta.stackoverflow.com/questions/57155/gravatar-bugs-and-improvements-in-chat/57170#57170.
I'm having troubles remembering how to grab the list view's state, or page, so that when you leave the list, you can return to the list view page you left on. I have a data pager, and I have hyplerlinks for items in the list view that take you to a file upload or file delete page, and then returns to the list view. It returns to the list view's initial state (first page).
I have a list of items(surveys) displayed on my home page. When I click the edit button for a particular item I have a modal pop up with the items details to edit. When the user clicks Save I submit the form via ajax. Depending on whether ModelState.IsValid == true I want to update the modal with validation information or close the modal and update the list of items with the new information.
This is how I am submitting the form:
[Code]...
My Questions
The only thing I can think to do is return JSON from my controller with a flag indicating the state of the ModelState.IsValid and the corresponding partial that I should show.
1) How would I do this?
2) Is there a better way?
Update
I found this: [URL]
but it seems more likely that I am going about the whole thing incorrectly.
I've got and SqlDataSource bound to a GridView, and I'd like to return the value of one of the fields from the SqlDataSource during GridView.RowCreated (I think). At the same time, one of the fields I'm returning to the GridView has a Null value, which I'm cool with, but Runtime is complaining about it so how can I get around errors relating to Null values in my tables?
When adding a stored procedure into the Entity Data Model I can select whether the procedure returns a scalar, a (new) complex type or one of the entity types I already defined.I mean assuming I have a view like this
CREATE VIEW FilteredFoos as SELECT Foo.* FROM Foo join ... WHERE ...(that is a view that implements some involved filtering, but returns all columns from one table) how do I add it to the project so that I can use the entity set, but get the Foo objects, not some new FilteredFoo objects.
var foos = myDB.FilteredFoos.Include("Bar").ToList(); foreach (Foo foo in foos) { ...
I have the following to get the Json abject passed from the controller and populate the various textboxes in the view. However, nothing is happening even though controller is passing a valid Json object. What is wrong with this code?
<script language="javascript" type="text/javascript"> $(document).ready(function() { var url = '<%=Url.Action("DropDownChange") %>'; $("#vendorID").change(function() { var selectedID = $(this).val(); if (selectedID != "New Vendor Id") { //$.post('Url.Action("DropDownChange","Refunds")', function(result) { $.post(url, { dropdownValue: selectedID }, function(result) { alert(selectedID); $("#name").val(result.Name); $("#city").val(result.City); $("#contact").val(result.Contact); $("#address2").val(result.Address2); $("#address1").val(result.Address1); $("#state").val(result.State); $("#zip").val(result.Zip); }); } }); }); This is the code in my controller; public JsonResult DropDownChange(string dropdownValue) // This action method gets called via an ajax request { if (dropdownValue != null && Request.IsAjaxRequest() == true) { paymentApplicationRefund = cPaymentRepository.PayableEntity(dropdownValue); paymentApplicationRefund.Address1.Trim(); paymentApplicationRefund.Address2.Trim(); paymentApplicationRefund.Name.Trim(); paymentApplicationRefund.City.Trim(); paymentApplicationRefund.Contact.Trim(); paymentApplicationRefund.State.Trim(); paymentApplicationRefund.Zip.Trim(); return Json(paymentApplicationRefund,"application/json"); } else { return null; } }