我正在对传递 FormData() 的控制器进行 ajax 调用,它有一个对象数组以及一些其他属性。在我的控制器中,我传递的列表数组似乎有 0 个元素。请帮忙!
cshtml 视图中的脚本 -
var _getFormDataToJson = function () {
var applyDetail = [];
$(_tb).find('tbody tr').each(function (i, v) {
var trans = {
effectiveDate: $(this).find('.effectiveDate').val(),
amount: $(this).find('.amount').val(),
empLeaveHdID: $('#tx-leaveHdID').val(),
//attachmentUrl: $(this).find('.leaveAttachment')[0].files[0]
}
applyDetail.push(trans);
});
var formObj = new FormData();
formObj.append('remark', $('#tx-remark').val());
formObj.append('leaveAppType', $('#hdnLeaveAppType').val());
formObj.append('applyDetail', applyDetail); //this collection has 0 items in controller
return formObj;
}
var _sumbitForm = function () {
var formData2 = _getFormDataToJson();
$.ajax({
url: '@Url.Action("ApplyLeave", "Leave")',
type: 'POST',
processData: false,
contentType: …Run Code Online (Sandbox Code Playgroud) 即使在Ajax.BeginForm中,我指定了targetId(这是我的局部视图),它仍在替换完整视图。我想念什么吗?
ProductList.cshtml
<div class="form-style-1" style="float:left">
@using (Ajax.BeginForm("SearchProducts", "Product", new AjaxOptions()
{
HttpMethod="POST",
InsertionMode = System.Web.Mvc.Ajax.InsertionMode.Replace,
UpdateTargetId = "product-list-container"
}))
{
<form>
<input name="SearchText" id="SearchText" class="field-divided" style="width: 300px;" type="text" />
<input type="submit" value="Search" class="myButton" />
</form>
}
</div>@Html.Partial("ListPartialView", Model)
ListPartialView.cshtml -
<div id="product-list-container">
@foreach (var product in Model)
{
<div class="product_box margin_r40">
<div class="thumb_wrapper"><a><img src="@Url.Content(@product.ImagePath)" alt="image 1" /></a></div>
<h3>@product.ProductName</h3>
<p>@product.Description</p>
</div>
}
<div class="button_01"><a href="#">View All</a></div>
<div class="cleaner"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
SearchProducts-控制器动作
[HttpPost]
public ActionResult SearchProducts()
{
var searchTxt = Request.Form["SearchText"];
IEnumerable<Product> searchedProducts …Run Code Online (Sandbox Code Playgroud) AJAX call -
Run Code Online (Sandbox Code Playgroud)
这里countryId作为"AU"传递 - 例如 -
$("#Countries").change(function () {
var countryId = this.value;
$.ajax({
url: '/Registers/GetStates',
type: "POST",
data: { 'data': countryId },
success: function (states) {
$("#States").html(""); // clear before appending new list
$.each(states, function (i, state) {
$("#States").append(
$('<option></option>').val(state.Id).html(state.Name));
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
调用GetStates方法后,'countryId'始终在控制器方法中作为null传递.不知道我在这里缺少什么.我也尝试了JSON.Stringify({'data':countryId}).谁能帮我吗 ?
Controller Action -
[HttpPost]
public SelectList GetStates(string countryId)
{
return Helper.GetStates(countryId);
}
Run Code Online (Sandbox Code Playgroud) IHttpControllerSelector和IHttpControllerActivator有什么区别?