vendredi 3 avril 2015

Populate TextArea based on SelectedItem in DropDownList - What's the better solution?


Vote count:

0




I want to populate a text area in a view based on the selected item in a dropdown list, but I'm not quite sure how to do it efficiently.


The dropdown list is created using ...



@Html.DropDownListFor(m => m.Item, new SelectList(Model.Items, "Id", "Text"))


I came up with 2 (well, more like 1 1/2) solutions:


1)


Items is a list consisting of SomeItem objects:



public class SomeItem
{
public int Id { get; set; }

public string Text { get; set; }

// AnotherText is to be populated with a default text,
// which can be changed and is later to be returned to the controller later
public string AnotherText { get; set; }
}


Whenever the selected item in the dropdown list changes, I change the text in a text area using the property "AnotherText":



$(document).ready(function () {
$('#Item').on('change', function () {
var getval = // get property "AnotherText" from selected item (how?)
$('#textArea').html(getval);
});


2)


Items is a list consisting of SomeItem objects:



public class SomeItem
{
public int Id { get; set; }

public string Text { get; set; }
}


Whenever the selected item in the dropdown list changes, I get the text for the text area from the controller, based on the selected item id:



$(document).ready(function () {
$('#Item').on('change', function () {
getDescription($(this).val());
});

function getDescription(id) {
$.ajax({
dataType: "json",
type: "GET",
url: "Home/Description/" + id,
success: function(data)
{
$('#textArea').html(data.Text);
},
error: function () {
alert("error");
}
});
}


Thanks for your feedback!



asked 20 secs ago







Populate TextArea based on SelectedItem in DropDownList - What's the better solution?

Aucun commentaire:

Enregistrer un commentaire