-1
從控制器發送List以查看的最佳方式是什麼。順便說一下,我是MVC的新手。 我想發送我的companyList(在崗位控制器)查看。 如果你問爲什麼我想在視圖中顯示companyList [0] .Name,並且我想在我的ajax中的「成功」狀態下執行此操作。發送列表控制器查看
謝謝。
這是我的控制器代碼對於獲取
public ActionResult Index()
{
KargoDB database = new KargoDB();
var query = database.States.Select(s => new SelectListItem
{
Value = s.State_id.ToString(),
Text = s.Description,
Selected = s.State_id.Equals(1)
});
var model = new State()
{
lstStates = query.AsEnumerable()
};
return View(model);
}
這是我的控制器代碼對於郵政
[HttpPost]
public ActionResult Index(string senderAddress, string senderState, string receiverAddress, string receiverState,
string receiverName, string receiverPhoneNumber, string requestDate, string cargoHeight, string cargoLenght,
string cargoQuantity, string paymentType, string cargoWidth, string cargoWeight)
{
RouteDetailRepository rdr = new RouteDetailRepository();
int routeId = rdr.getRouteId(senderState, receiverState);
List<RoutesDetail> companyList = rdr.getRouteDetail(routeId);
if (companyList == null)
{
return JavaScript("Seçilen Güzergaha Sistemde Kayıtlı Olan Hiç Bir Kargo Firması Servis Yapmamaktadır.");
}
return Json(new { status = "success" });
}
,你可以看到我的成功返回我的AJAX。
這是我查看代碼的一部分
@model denemee.Models.State
<script type="text/javascript">
$(document).on("click", "#btnSearch", function() {
var senderAddress = document.getElementById("senderAddress").value;
var senderState = document.getElementById("senderState").value;
var receiverAddress = document.getElementById("receiverAddress").value;
var receiverState = document.getElementById("receiverState").value;
var receiverName = document.getElementById("receiverName").value;
var receiverPhoneNumber = document.getElementById("receiverPhoneNumber").value;
var requestDate = document.getElementById("datepicker").value;
var cargoHeight = document.getElementById("cargoHeight").value;
var cargoLenght = document.getElementById("cargoLength").value;
var cargoQuantity = document.getElementById("cargoQuantity").value;
var paymentType = document.getElementById("paymentType").value;
var cargoWidth = document.getElementById("cargoWidth").value;
var cargoWeight = document.getElementById("cargoWeight").value;
if (senderAddress === "" || senderState === "" || receiverAddress === "" || receiverState === "" ||
receiverName === "" || receiverPhoneNumber === "" || requestDate === "" || cargoHeight === "" ||
cargoLenght === "" || cargoQuantity === "" || paymentType === "" || cargoWidth === "" ||
cargoWeight === "") {
alert("Lütfen Tüm Alanları Doldurunuz.")
} else {
$.ajax({
type: 'POST',
url: '/Main/Index',
dataType: 'json',
data: {
'senderAddress' : senderAddress,
'senderState': senderState,
'receiverAddress' : receiverAddress,
'receiverState' : receiverState,
'receiverName' : receiverName,
'receiverPhoneNumber' :receiverPhoneNumber,
'requestDate': requestDate,
'cargoHeight': cargoHeight,
'cargoLenght' : cargoLenght,
'cargoQuantity' : cargoQuantity,
'paymentType' : paymentType,
'cargoWidth' : cargoWidth,
'cargoWeight' : cargoWeight,
},
success: function (msg) {
},
error: function (msg) {
alert(msg.responseText);
}
});
}
});
</script>
謝謝你這麼多@AlbertoMonteiro –
@UfukDoğan任何時候,我高興,這是對你有用:d –
@UfukDoğan我看到你在做一些otimizations你代碼,你想讓我做回答嗎? –