I want to pass a value from the view to the controller the value is retrieve via jquery when I use the code below it does not work the value is null in the controller.
//Hardcoded to test
var groupId = 1;
$('#timeList').load('/Time/Index/' + groupId);
public ActionResult Index(int? groupId)
{
AddProjectToViewData(-1);
return View(_service.ListTimes());
}
Try this:
var groupId = 1;
$('#timeList').load('/Time/Index', {groupId: groupId}); //Send named parameter
[HttpGet]
public ActionResult Index(int? groupId)
{
AddProjectToViewData(-1);
return View(_service.ListTimes());
}
Hope this helps
assuming that you are using default routes in global.asax.cs you can retrieve this value in id
public ActionResult index(int? id)
{
AddProjectToViewData(-1);
return View(_service.ListTimes());
}
if you do wish to receive it in groupid you have to change jquery code a little
//Hardcoded to test
var groupId = 1;
$('#timeList').load('/Time/Index?groupid=' + groupId);
You want to do either a post or a get, and I think you may want to pass back a string or json. For ease, I'll show you a get with json:
///your javascript
var groupId =1;
$.get('Time/Index/' + groupId, function(data) {
$('#timeList').val(data);
///might need to do a for each since you are returning a list or whatever
});
///your controller logic
[HttpGet]
public string Index(int? groupId)
{
AddProjectToViewData(-1);
//might want to pass back json here
return WhateverYourDataIs.ToString();
}
Hope this helps
Related
In MVC I am trying to pass an optional ID parameter from one ActionResult method and I want to capture that ID in another ActionResult method. I currently have the following code but I still can't find a way to get the ID in the Method2().
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Method1(SomeModel model)
{
int someID = model.something.Id;
.
..
...
return RedirectToAction("Method2", new { userID = someID});
}
After clicking on a button on Method1's View page, the code will direct me to Method2 page and I will see something like this in my URL
http://localhost:1234/myController/method2?userid=100 note that the ?userid=100 was sucessfully passed to the URL when Method2 got called.
and this is my Method2. I am trying to get the userid but I can't.
[HttpGet]
public ActionResult Method2()
{
**I want to get the userID from the URL**
}
I even tried to use int? id but still I am getting a null for id.
public ActionResult Method2(int? id)
{
//id return null all the time
}
Any help on how I can get the userid in the URL in Method2()?
your query string's variable name and actionresult variable name must match
[HttpGet]
public ActionResult Method2(int userid)
{
//your code
}
or even
public ActionResult Method2(int? userid)
{
//id return null all the time
}
The value key value pair for userid is in the request querystring collection. To get it do something like the following:
if (Request.QueryString.Count > 0)
{
if(Request.QueryString["userid"] != null)
{
int userId = (int)Request.QueryString["userid"];
}
}
This is my first MVC app, and I'm wondering is it possible to pass list of a string to a Controller on this way, and if not I would like to know why it is not :
What I've tried to do here :
public List<string> Index(string id)
{
ViewBag.Peoples = new List<string>()
{
"Joe",
"John",
"Jennifer"
};
return View();
}
This is the error I'm getting :
Error 1 Cannot implicitly convert type 'System.Web.Mvc.ViewResult' to 'System.Collections.Generic.List<string>'
I'm wondering why I'm facing this issue, why I can't pass just list of a strings to Index view?
Your return type should be an ActionResult, generated by passing a viewmodel (containing properties such as the list of strings) as the parameter to the view that it should be bound to.
public ActionResult Multiple()
{
if(DateTime.Now.Day % 2 == 0)
{
return View("Other"); }
else
{
return RedirectToAction("Index");
}
}
Excuse formatting, on my mobile.
Tutorial here https://www.exceptionnotfound.net/asp-net-mvc-demystified-actionresults/amp/
you can use form collection method
public ActionResult Test(FormCollection collection)
{
string results = collection["Blanks"];
}
public ActionResult Index(string id)
{
ViewBag.Peoples = new List<string>()
{
"Joe",
"John",
"Jennifer"
};
return View();
}
Here Problem is that Your method return type is List<string> and you have returned View() so that is not possible. change method return type List<string> to ActionResult it should be works. thanks.
I am working on a project in asp.net mvc3(c#).
I need a solution for convert a view(not a partial view) to string from different controllers.
Code Explantion:
1) Calling "details" action of proposalmoduleController from proposalsController.
2) proposalmoduleController action "details" returns a view and convert this view(return result) as a string in proposalsController.
Code
public class proposalmoduleController : ControllerBase
{
[HttpGet]
public ActionResult details(int id, int widgetuniqueid)
{
//id - widgetid of div container
List<ModuleViewModel> listmoduleviewmodel = new List<ModuleViewModel>();
List<ModuleFieldViewModel> listmodulefieldviewmodel = new List<ModuleFieldViewModel>();
var objProposalModuleService = new ProposalModuleService();
var objModuleViewModel = new ModuleViewModel();
string WidgetTitle = "";
Int64 ModuleTemplateID = 0;
//objModuleViewModel.ProposalID = proposalid;
objModuleViewModel.ProposalModuleWidgetID = id;
listmoduleviewmodel=objProposalModuleService.Select(1, objModuleViewModel,out listmodulefieldviewmodel, out WidgetTitle, out ModuleTemplateID);
return View(listmoduleviewmodel);
}
}
public class proposalsController : ControllerBase
{
public string SaveHtml(int ProposalID)
{
var objProposalSortOrderViewModelList = new List<ProposalSortOrderViewModel>();
proposalmoduleController objModuleController = new proposalmoduleController(); // Initilize the object of proposalmoduleController for accessing details method
objProposalSortOrderViewModelList = GetProposalSortorders(ProposalID);
string result;
foreach (var item in objProposalSortOrderViewModelList)
{
ViewResult viewResult = (ViewResult)objModuleController.details(Convert.ToInt32(item.KeyID), Convert.ToInt32(item.SortOrder)); // Fetch the result returned from proposalmodulecontroller,details action
result=viewResult.ToString(); // Need to get result fetch from the proposalmodulecontroller,details action as a string
}
}
}
enter code here
Please suggest any solution.
A ViewResult is not a View. ViewResult is used by the MVC engine to determine the view that must be rendered.
I think it's better if you change your perspective:
if you want to include a partial view in a view just work on the presentation code using #Html.Partial
if you want to get the details data in your proposalsController don't call the action of the proposalmoduleController but call a service method that gives you the data
Is it possible to pass data from one controller to another? When my second controller is called the int id is showing the WRONG value:
I have one controller:
public ActionResult Index(int id)
{
return RedirectToAction("New", "NewProducts", id);
}
NewProductsController:
public string New(int id)// <--------id never has correct number when called
{
return "value: " + id;
}
You need to use either a RouteValueDictionary or an anonymous type with an id property for the route values otherwise it will use the properties on the Int32 object id to fill the route values dictionary.
public ActionResult Index(int id)
{
return RedirectToAction("New", "NewProducts", new { id = id } );
}
Hope it will work
public ActionResult Index(int id)
{
return RedirectToAction("New?id=" + id, "NewProducts");
}
this is my action "Index"
when i first go to page i dont have the "pageParent"
so i dont get the page.
just if i enter to it like this "http://localhost:50918/Page?pageParent=1" it's enter.
how to make "http://localhost:50918/Page" to work?
public ActionResult Index(int pageParent)
{
var id = pageParent;
var pages = (from p in db.pages
where p.pageParent == id
select p);
PageParentModel model = new PageParentModel();
model.page = pages;
model.pageParent = id;
return View(model);
}
Modify your Action like this
public ActionResult Index(int? pageParent) {
// this way your pageParent parameter is marked to be nullable
// dont forget to check for the null value in code
}
You can set a default value for use too in the case that the parameter isn't supplied in the querystring - e.g.:
public ActionResult Index([DefaultValue(1)] int pageParent) {
}