MVC :: Methods To Passing Additional Data From Controller To View
Apr 29, 2010
I am getting started with Ap.net MVC. For that i chose to practice it by build an application. Im using MVC 2 and Linq To SQL, and i would like to passing another Query to the view. For example, i have this:
[Code]....
So i would like to pass data1 and data2 to the View. I can use return View(data1), but the View function accept just one data. So what technique i can use to pass the tow data to the view
I have setup an httppost that sends a string into my controller, searches for some results using linq, and then sends the results to a view. In debug stepping through the code I can see the data that I am looking for being passed into the return view statement, but the page just appears to refresh (it doesn't render the view as expected with the result). why my controller fails to redender the view? (note: I didn't include the view because I can send a ToList() to it without an issue. For example, return View(_entities.Persons.ToList());
[HttpPost] public ActionResult RenderSearch(string usersearchtext) { if (usersearchtext != null) { var search_results = from s in _entities.Persons where s.Description.Contains(usersearchtext) select s; return View("SearchResult", search_results.ToList()); } else { throw new NotImplementedException(); } }
I have a dictionary I'm passing to a View. I want to be able to pass the values of that dictionary back to a Controller action from this same View. Is there anyway this can be accomplished without using a form? The situation is that I need to be able to pass these back to a controller action that is called when a user clicks an ActionLink from the View, and in my experience an ActionLink cannot be used to submit a values of a form, i.e. I've only been able to submit values of a form using the form's submit button. Unless there's a way to use an ActionLink to submit values in a form.Controller Action passes Dictionary to Controller in ViewData:
public ActionResult ModifyNewCrossListing(FormCollection form) { Dictionary<int, string> prefixlist = new Dictionary<int, String>();
I have a table with a ID, CodeID,LibelleID field. I am using oracle connection. i want to display in a drop downlist (textfield) CodeID+LibelleID for exemple "100-SalaryBase" where CodeID=100 and LibelleID="SalaryBase" is it possible to obtain it if the user selects an element from the dropdownlist how to post both the codeID and ID to the controller
I have following situation - I am pasing user info object from Controller to View. It contains GUID UserID, which i dont want to be seen on page. So I removed every Html.LabelFor(model => model.UserID), Html.TextBoxFor(model => model.UserID) etc... from generated View source. And because of this when Html.BeginForm() returns that object back to Controller all values is there but UserID is lost??
If I leave Html.LabelFor(model => model.UserID), Html.TextBoxFor(model => model.UserID) etc.. in View everything is fine. But I dont want to show UserID? Where is the problem here?
I am trying to pass a view model of mine, which has multiple list in it, to my view. Then in my view i need to edit the different list. Then on my post i need to save the edits. Although, when i pass my viewmodel back to my post, it is empty! Can somebody explain what i am doing wrong? Controller
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; } }
I would like to pass List<SelectListItem> (not selected item but the whole "List" object) back to controller.
for example in my GET controller i would have this
ViewData["list"] = some select list from repository;
then on post I would like to get the list back from view..pretty much i'm trying to use ViewData["list"] as storage..this way I would use ajax to remove or add items to/from the list.
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 to pass a list of Entity Objects from Controller to View but not put it in the Model context. It is the users homepage, but I am putting a list of alerts on the users home page(somewhat like facebook). So I need the User Context as the Model I pass but need to pass the Alert model as well so I can show the list of alerts.. How would I do that? The code below is what I have so far..
I have a form that I don't want to post back so I need to get the form values into my controller via jquery's ajax method.Here is my test controller method:
[Code]....
When I debug the controller method and look at the lineItems parameter, it always contains 0 items. I've tried various formats for the javascipt lineItems parameter but still 0 items. I'm also open to some other way of getting these values in like the jquery form serialze method or something else.
I have one controller with 4 to 5 action method. In all action method i have to check some feilds from database. So to reduce the database query i want to save this data one time and then want to use that in all action method. I know about tempdata and view bag but none of this can use in different action method, so how to achive this.
I am new to MVC, and trying something and got stuck somewhere in between.I have a user control there I have three textbox html type(ID, Lastname, firstname) and a submit buttom. I set the button like
I have called this usercontrol on some view through
<%= Html.Partial("ucName") %>
Now on pressing that button(on user control) I need to pass the data from these textboxes to controller again to some specific action(Http Post action). By using this data I want to do some database interaction and storing the result in a dataset and pass this data set to same view again to show up in some Grid.I know the first part in conventional Asp.net can be done by raising the event through delegate but don't know how to do that in MVC.
Can i create a partial view and a controller that will feed data to the view, and if i render that partial in a Master page, the Data will show on whatever URL i am?
Or is there another way of showing content from database on every page(view)?
Basically, I have an HTML Form and would want to pass the data from the form to n asp.net mvc controller, then the controller would return an XML for client side manipulation.
Here is my initial code:
[Code]....
When I run and debug, I get a message that says "attr(..) is null or not an object. I am still trying to learn web development using ASP.NET MVC.
I have a view which takes two objects: booking and list of reasons for canceling that booking.I have two classes: clsBooking, clsBookingCancelationReason I can read both objects in my view - no problems there. After I read the objects, I display the booking details and I generate a list of cancelation reasons in the following way:
[Code]....
The code above generates a list of cancelation reasons.How do I pick up the selected ReasonId from the list? I need to generate a link that will contain the bookingId and the selected reason for canceling the booking.I can get the bookingId out easily since it's stored in the Model...but how do I go about the selected ReasonId?
i have a repository class inside my model folder; this repositiry contains many methods , which are called from Action methods in the controllers; so my question is :- if i have a method insidle my model.reporsitory which calls a stored procedure, then can i call it an "action method"? or "action method" expression only apply to controller methods?
I am having two methods inside a controller like1.public ActionResult ProfileView(int UID){}2.public ActionResult UpdatingAthleticInfo() { }How to call ProfileView method and pass UID parameter from UpdatingAthleticInfo method ??
I have a simple model where a Person has Gifts. I have a view which is a list of Gifts belonging to one Person.
My problem is with the Create action for a new Gift. I want it to default to the PersonID that we are already viewing the list of Gifts for. I tried simply passing the last PersonID (they are all the same)
Html.ActionLink("Create New", "Create", new { id = Model.Last().PersonID }) which works fine if there is already at least one Gift for that person but if this is the first Gift I don't have a value.
My Gift List controller knows the PersonID I want to pass but the view doesn't.
How do I pass this PersonID from my Gift List controller to my Gift Create controller via the Gift List view? Or is there a better way to do this?
Working on a heavy ajax based site with over 100 methods from all the ajax calls inside each of each controller all with basically the same code.Was thinking of changing them to a big case statement for readability which would keep the code a bit more dry and make it easier to read.What I am wondering is: Will there be a performance hit and is there a better way to deal with it?
I am attempting to implement a custom locale service in an MVC 2 webpage. I have an interface IResourceDictionary that provides a couple of methods for accessing resources by culture. This is because I want to avoid the static classes of .Net resources.The problem is accessing the chosen IResourceDictionary from the views. I have contemplated using the ViewDataDictionary given, creating a base controller from which all my controllers inherits that adds my IResourceDictionary to the ViewData before each action executes.
(ViewData["Resources"] as IResourceDictionary).GetEntry(params);