1
아래와 같은 UI를 만들었습니다. 사용자가 드롭 다운 목록에서 국가를 선택하면보기가 동적으로 반환됩니다. 이보기에는 주 이름, 중요한 도시 목록 및 설명을 입력 할 수있는 텍스트 상자가 있습니다.ASP.NET MVC -보기에서 컨트롤러로 사용자 입력 전달
사용자가 드롭 다운 목록에서 몇 가지 값 (여러 개)을 선택하면 몇 가지 설명을 입력하고 제출 단추를 누르면 이러한 데이터가 추가 처리를 위해 컨트롤러로 다시 전달됩니다. 다음은
내 모델 : 아래public class CountryModel
{
public string Country { get; set; }
public string State { get; set; }
public List<string> Cities { get; set; }
public static IEnumerable<CountryModel> GetLocationDetails()
{
return new List<CountryModel>()
{
new CountryModel { Country = "India", State = "WB", Cities = new List<string>(){ "Kolkata", "Kharagpur", "Darjeeling" } },
new CountryModel { Country = "India", State = "AP", Cities = new List<string>(){ "Hyderabad", "Vizag", "Vijaywada" } },
new CountryModel { Country = "India", State = "UP", Cities = new List<string>(){ "Kanpur", "Allahabad", "Agra" } },
new CountryModel { Country = "India", State = "MH", Cities = new List<string>(){ "Mumbai", "Pune", "Nagpur", "Nasik", "Aurangabad" } },
new CountryModel { Country = "India", State = "RJ", Cities = new List<string>(){ "Jaipur", "Kota", "Jaisalmer" } },
new CountryModel { Country = "USA", State = "CA", Cities = new List<string>(){ "San Francisco", "Los Angeles", "Oakland" } },
new CountryModel { Country = "USA", State = "WA", Cities = new List<string>(){ "Seattle", "Bellevue", "Yakima" } },
new CountryModel { Country = "USA", State = "NY", Cities = new List<string>(){ "New York City", "Buffalo", "Albany" } },
};
}
public List<SelectListItem> Countries { get; set; }
public string Comments { get; set; }
}
컨트롤러 코드 :
public class CountryController : Controller
{
public ActionResult Select()
{
CountryModel viewmodel = new CountryModel();
viewmodel.Countries = new List<SelectListItem>()
{
new SelectListItem { Text = "India", Value = "India", Selected = true },
new SelectListItem { Text = "USA", Value = "USA" }
};
return View(viewmodel);
}
public JsonResult GetCountryDetails(string id)
{
var query = from c in CountryModel.GetLocationDetails()
where c.Country == id
select c;
return Json(new { query }, JsonRequestBehavior.AllowGet);
}
}
그리고보기는 다음과 같습니다 :
model MvcApplication2.Models.CountryModel
@{
ViewBag.Title = "Select";
}
<h2>Select</h2>
@using (Html.BeginForm())
{
@Html.Label("Select a Country: ")
@Html.DropDownListFor(m => m.Countries, Model.Countries)
<div id="dynamictable"></div>
<script type="text/javascript">
$(document).ready(function() {
$('#Countries').change(function() {
var url = '/Country/GetCountryDetails/' + $('#Countries').val();
$.getJSON(url, function (result) {
if (result.query.length > 0) {
if ($('#tblResult').length) {
$('#tblResult').remove();
}
$('#dynamictable').append('<table border="1" id="tblResult"></table>');
var table = $('#dynamictable').children();
table.append('<th>State</th> <th>City</th> <th>Comments</th>');
var random = 1;
$.each(result.query, function (i, location) {
var ddlId = "citiesInState";
var finalDdlId = ddlId.concat(random.toString());
var markup = '<tr class="locationInfo"><td class="stateCode">' + location.State + '</td><td class="citiesList"><select id="' + finalDdlId + '"></select></td><td class="userCommentsOnLocation">@Html.TextBoxFor(m=>m.Comments)</td></tr>';
table.append(markup);
var option = '';
for (var i = 0; i < location.Cities.length; i++) {
$('#' + finalDdlId).append('<option value="' + location.Cities[i] + '">' + location.Cities[i] + '</option>');
}
random++;
});
}
});
});
});
</script>
<br />
<input type="submit" value="Submit" id="SubmitId" />
}
문제 그는 컨트롤러에 주석 만 전달되는 것입니다. 그러나 드롭 다운 목록에서 선택한 값이 컨트롤러로 전달되지 않습니다. 다음은 POST 액션 메소드입니다.
[HttpPost]
public ActionResult Select(CountryModel cm)
{
if (cm == null)
{
throw new ArgumentNullException("cm");
}
return View("Display", cm);
}
컨트롤러에서 선택한 값을 모두 얻는 방법은 무엇입니까? 어떤 도움을 주셔서 감사합니다.
나는이 기능을 제공한다고 생각하지 못했습니다 ... – SKJ