我在表单上有一个取消按钮,假设(重新)AJAX原始页面...问题是它将页面空回送而没有任何数据。我无法弄清楚我做错了什么。 这是我的代码:
$("#cancel-price-button").click(function() {
$("#client-pricing-container").html("<h1 style='color:grey'>Loading...</h1>");
$.ajax({
type: "GET",
url: '@Url.Action("GetProjectPricing","Project",Model?.Project?.ProjectPricings)',
success: function(data) {$("#client-pricing-container").html(data); },
error: function(data) {
$("#client-pricing-container").html("<h1 style='color:red'>Ajax Error!</h1>");
console.log('An ajax error occurred.');
console.log(data);
}
});
});
这是我的控制者:
[HttpGet]
public ActionResult GetProjectPricing(string returnMessage,string returnClass,int?id=null)
{
if (id == null)
{
var pricingDetail = ProjectDataAccess.GetProjectDetails(null);
pricingDetail.Project = new Project();
pricingDetail.ReturnMessage = returnMessage;
pricingDetail.ReturnClass = returnClass;
return PartialView("Partial/ClientPricing", pricingDetail);
}
var pricingDetails = ProjectDataAccess.GetProjectDetails(id.Value);
if (pricingDetails == null) return PartialView("Error");
pricingDetails.ReturnMessage = returnMessage;
pricingDetails.ReturnClass = returnClass;
return PartialView("Partial/ClientPricing", pricingDetails);
}
答案 0 :(得分:0)
这样的问题通常是由于API方法所期望的参数的形状或命名与您从视图中传递的参数之间的差异造成的。
在您的情况下,您的API需要:
GetProjectPricing(string returnMessage, string returnClass, int? id=null)
您传递了以下对象:
Model?.Project?.ProjectPricings
因此,假设您已确保对象(或其父项)不为null,则需要确保ProjectPricings的结构与预期形状匹配并具有预期属性。如果没有(或者如果你不是肯定的话),试试这个:
@Url.Action("GetProjectPricing","Project", new { returnMessage = "some text2", returnClass = "someClass", id = 9})
&#39; ID&#39;可以省略,因为您的方法签名中有默认值