I am returning list<user> record through mvc controller, as jquery is client side script I dont know how to populate <select></select> with the model value.
@model List<YourProject.YourModels.User>
<!-- Renders a DropDownList for your collection -->
@Html.DropDownList("NameOfYourList", new SelectList(Model, "ValuePropertyOfModel", "DisplayPropertyOfModel"))
More detailed example
If your class looks like this :
public class User
{
public string Name { get; set; }
public string ID { get; set; }
}
which might be populated through your Controller Action :
public ActionResult YourAction()
{
//Populates a List of Users
List<User> users = GetYourUsersHere();
//Passes your collection to the View
return View(users);
}
and within the View you could populate a drop-down (<select> element) that would contain each object in your collection and map it to a single <option> within your <select> :
@model List<YourProjects.Models.User>
<!-- Each element will display the Name property and it's value will be set to the Id property -->
@Html.DropDownList("NameOfYourList", new SelectList(Model,"Id", "Name"))
public ActionResult GetSite()
{
var sites = new[]
{
new { Id = "1", Nome = "site 1" },
new { Id = "2", Nome = "site 3" },
new { Id = "3", Nome = "site 3" },
};
return Json(sites, JsonRequestBehavior.AllowGet);
}
try this
Thanks & Regards
Raman C.
Marked as answer by nelson123 on Feb 22, 2013 02:03 PM
nelson123
Member
21 Points
26 Posts
how to populate MVC model value in html <select> field through jquery ajax
Feb 15, 2013 12:09 PM|LINK
I am returning list<user> record through mvc controller, as jquery is client side script I dont know how to populate <select></select> with the model value.
Web Developer
Mumbai
Rion William...
All-Star
26554 Points
4414 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 15, 2013 12:23 PM|LINK
You can use the @Html.DropDownList() or @Html.DropDownListFor() helper methods, which will generate the appropriate <select> objects by passing in your collection :
@model List<YourProject.YourModels.User> <!-- Renders a DropDownList for your collection --> @Html.DropDownList("NameOfYourList", new SelectList(Model, "ValuePropertyOfModel", "DisplayPropertyOfModel"))More detailed example
If your class looks like this :
public class User { public string Name { get; set; } public string ID { get; set; } }which might be populated through your Controller Action :
public ActionResult YourAction() { //Populates a List of Users List<User> users = GetYourUsersHere(); //Passes your collection to the View return View(users); }and within the View you could populate a drop-down (<select> element) that would contain each object in your collection and map it to a single <option> within your <select> :
@model List<YourProjects.Models.User> <!-- Each element will display the Name property and it's value will be set to the Id property --> @Html.DropDownList("NameOfYourList", new SelectList(Model,"Id", "Name"))nelson123
Member
21 Points
26 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 18, 2013 06:41 PM|LINK
this I know. But how to populate it in <select></select> through .ajax get
Web Developer
Mumbai
asteranup
All-Star
30184 Points
4906 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 19, 2013 04:31 AM|LINK
Hi,
Try this-
http://forums.asp.net/p/1806541/4993694.aspx/1?Re+assigning+json+objects+to+variables
https://delicious.com/anupdg/ajax+mvc
Anup Das Gupta
Mark as Answer if you feel so. Visit My Blog
bruce (sqlwo...
All-Star
36656 Points
5438 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 20, 2013 04:07 AM|LINK
you don't say what 2 columns out of user go into the list, but lets assume id and name. then its just:
$.ajax(url, function(r) { $list = $('#selectName'); $list.empty(); r.each(function(i,v) { $('<option></option>') .prop('value',v.id) .text(v.name) .appendTo($list); }); });Raman.C
Member
108 Points
19 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 20, 2013 01:16 PM|LINK
your view
@Html.DropDownListFor(x => x.testid, new SelectList(Enumerable.Empty<SelectListItem>())) $(function() { $.getJSON('@Url.Action("GetSite", "Pedido")', function(result) { var ddl = $('#testid'); ddl.empty(); $(result).each(function() { ddl.append( $('<option/>', { value: this.Id }).html(this.Nome) ); }); }); });public ActionResult GetSite() { var sites = new[] { new { Id = "1", Nome = "site 1" }, new { Id = "2", Nome = "site 3" }, new { Id = "3", Nome = "site 3" }, }; return Json(sites, JsonRequestBehavior.AllowGet); }Raman C.
nelson123
Member
21 Points
26 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 21, 2013 07:54 PM|LINK
But I need to return Json to another view.
for example if we are using,
public ActionResult GetSite()
{
return json();
}
but I want results in view 'Addsite' rather than 'GetSite',
how can we do this ?
Web Developer
Mumbai
medelbrock
Member
627 Points
133 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 21, 2013 09:45 PM|LINK
You can call any of your Actions from any page as long as it obeys the Same Origin Policy
nelson123
Member
21 Points
26 Posts
Re: how to populate MVC model value in html <select> field through jquery ajax
Feb 22, 2013 02:21 PM|LINK
I want to show the text (var status='Login Successful') return in controller in View through jquery ajax
eg:
Web Developer
Mumbai