viewbag

how to pass data from View to Controller using ajax get or post in mvc with parameters

二次信任 提交于 2019-12-10 15:48:00
问题 I am trying to pass data from View to Controller Action Method using ajax as follows:- I have Membership instance of user which I passed in from another controller to this view below using viewbag somewhat like this ViewBag.MyUser = MyUser; Now I want to pass 'MyUser' to another Controller form this view using ajax as below. $('#Link').click(function () { $.ajax({ url: http://localhost/Account/Process, type: 'POST', data: '@ViewBag.MyUser', success: function () { }, error: function () { } });

How can ViewBag data be saved after a form post?

淺唱寂寞╮ 提交于 2019-12-10 03:05:55
问题 So I have a ViewBag.Something and this data is randomly generated. In my view, I set this to a label like so @Html.LabelFor(m => m.Something, (string)ViewBag.Something) . This works out well but when I submit the form there could be errors and if there are errors, I need this label to remain the same, I don't want dynamic/random data anymore so I wouldn't call the controller method that generated this ViewBag. Is there a way to retain this value without having some private variable in my

Setting up the value in ViewBag using Jquery

大憨熊 提交于 2019-12-09 08:04:07
问题 I just want to ask, is there a way to set up the viewbag value dynamically using jquery? I try this code in my script, $(".btn").on("click",function(){ @ViewBag.Id = $(this).attr("id") }); i dont know if its correct but when i try to run my MVC 3 Project, this error appear in my firebug Syntax Error = $(this).attr("id") Please Help. Thanks 回答1: You're misunderstanding how the ViewBag works. When working in MVC, and you open a webpage, this is what (roughly) happens: The Method 'Index' of

Returning a list to strongly typed view using viewbag

孤街浪徒 提交于 2019-12-09 07:35:25
I am trying to return a list of customers to a strongly typed view using the viewbag and am having some difficulties as im new to MVC and Razor, can anyone kindly offer any advice on the following problem please? I have the following Code in my controller, public ViewResult Index() { var q = from a in db.Customers select a; ViewBag.customer = q; return View(db.CustomerSites.ToList()); } and this code in my view @foreach (var customer in Model) { <tr> <td> @ViewBag.customer.CustomerName <td> <td> @customer.UnitNo </td> <td> @Truncate(customer.StreetName, 25) </td> <td> @Html.ActionLink("Edit",

How to display value of a ViewBag in my view with a JS function?

无人久伴 提交于 2019-12-08 07:00:39
问题 I want to display the data from a ViewBag in my View with Javascript. Here is my code. View <span id='test'></span> Javascript function myFunction() { $('#test').text('@ViewBag.Test'); } When myFunction() is called I get the text @ViewBag.Test but not his value. How can I fix this ? 回答1: You need to place your JavaScript which takes the @ViewBag.Test value in a page which is interpreted by the Razor view engine. My guess is that this is currently not the case. If you want to keep your

How to display value of a ViewBag in my view with a JS function?

夙愿已清 提交于 2019-12-07 11:58:33
I want to display the data from a ViewBag in my View with Javascript. Here is my code. View <span id='test'></span> Javascript function myFunction() { $('#test').text('@ViewBag.Test'); } When myFunction() is called I get the text @ViewBag.Test but not his value. How can I fix this ? You need to place your JavaScript which takes the @ViewBag.Test value in a page which is interpreted by the Razor view engine. My guess is that this is currently not the case. If you want to keep your javascript codebase separate from the view (which is entirely reasonable) you can use a global variable: // in the

Null TempData when passing data from controller to view MVC

你离开我真会死。 提交于 2019-12-07 11:36:32
问题 I have the following class in a Controller passing data to a View: public ActionResult ControllerToView(){ ... TempData["example"] = "this is a message!"; ... return Redirect("http://myViewPageLink"); } In my View, I am trying to access the TempData dictionary with: @if(myCondition){ var test = TempData["example"]; <p>@test</p> } "myCondition" is always satisfied, but the TempData dictionary is always empty. Any ideas why? Is there any aditional code I have to write in order to make TempData

It is possible to refresh ViewBag value in the view?

不羁的心 提交于 2019-12-06 14:32:19
问题 I'm trying to do a dynamic dropdownlist : I get the options of the dropdownlist from a database and put them in an objects list. In accordance with a checkbox value i remove objects from the list and set this list as a ViewBag value. public ActionResult ThematicManagement(string Id, string IsAdult, string flagAdult) { ..... ViewBag.DDL = null; var response = VodCatalogBUS.GetParentThematics(); List<oboThematic> list = new List<oboThematic>(); list = response.Data; if (IsAdult == null ||

Why does an MVC view need to exist in the View directory to work?

血红的双手。 提交于 2019-12-06 05:52:29
问题 I have been writing a cms with MVC being used as the main engine for generating the pages. I am going well, but wanted the ability to create a unique razor template per site and possibly per view if I need to. My rules are that each project has to have a unique code which is linked with a url. Assets for each project site are stored in a way that the location relates to the project. So an assets associated with project C0001 would be stored in assets\C0001\ and for C0002: assets\C0002\ and so

Effectively avoiding ViewBag in ASP.NET MVC

▼魔方 西西 提交于 2019-12-06 03:32:25
问题 How do you deal with avoiding ViewBag due to its risk of error with being dynamic but also avoid having to populate a new ViewModel and pass it back to the view each time. For instance, I don't want to necessarily change the follow to expose common data normally stuffed in ViewBag. [HttpGet] void Index() { return View(); } to [HttpGet] void Index() { var messages = new MessageCollection(); messages.AddError("Uh oh!"); return View(messages); } Where in the pipeline would I add a property like