I am calling my controller method using .ajax. my controller method call web service which returns dictionary. now i need to return this and populate dropdown list. i am trying with return JSON and need to populate using success (response)
I am using MVC 1.0
$.ajax(
{
url: 'LookupValue/',
data: { 'sLookupIds': selectedtext },
datatype: "json",
traditional: true,
success: function (data) {
alert(data.value);
}
});
thanks in advance.
Right click on Home folder inside the View folder in the created MVC application as in the following screenshot: Give the name EmpDetails and click Add button. To bind view using json we need JQuery and the following JQuery library to communicate to the controller from view: <script src="~/Scripts/jquery-1.10.
JsonResult is an ActionResult type in MVC. It helps to send the content in JavaScript Object Notation (JSON) format.
The Controller Action method will be called using jQuery POST function and JSON data will be returned back to the View using JsonResult class object. Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
In controller
public JsonResult LookupValue(String sLookupIds)
{
SelectList olist = new SelectList(oDict, "key","value");
return Json(olist);
}
In view
$.ajax(
{
url: 'LookupValue/',
data: { 'sLookupIds': selectedtext },
datatype: "json",
traditional: true,
success: function (data) {
$.each(data, function (index, val) {
$('#lookup')
.append($("<option></option>")
.attr("value", val.Value)
.text(val.Text));
//ddHTML = ddHTML + "<option value='" + val.Value + "'>'" + val.Texts + "'</option>";
});
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With