[英]ASP.NET MVC5 problem in passing data between Controllers
我正在構建一個基本的汽車租賃應用程序。 用戶可以查看汽車並單擊“ Rent
按鈕。 單擊它之后,我需要返回一個新的View
,其中包含一個表單,用戶必須填寫該表單才能完成訂單。 為了完成Rent
,我在控制器之間傳遞Car
數據以及Customer
數據時遇到問題。
在主頁上,每輛車下面都有一個“ 租金”鏈接。 這是代碼:
<div class="col-md-12">
<p>@Html.ActionLink("Rent", "Rent" , new { Id = car.Id})</p>
</div>
來自HomeController
Rent
方法
public ActionResult Rent(string id)
{
return RedirectToAction("Create", "Rents");
}
從RentsController
Create
方法
[HttpPost]
public ActionResult Create(string carId, Rent rent)
{
if (!ModelState.IsValid)
return View();
var carToRent = context.Cars.SingleOrDefault(c => c.Id == carId);
if (carToRent == null)
return Content($"Car not found!");
rent.Car = carToRent;
var customer = context.Customers.SingleOrDefault(c => c.UserId == User.Identity.Name);
if (customer == null)
return Content($"Customer not found!");
rent.Customer = customer;
context.Rents.Add(rent);
context.SaveChanges();
return RedirectToAction("Index");
}
每次嘗試訪問Rents/Create
時,我都收到HTTP 404錯誤。
如下所示,您可以在RedirectToAction()方法中傳遞參數。
RedirectToAction(String, String, RouteValueDictionary)
使用操作名稱,控制器名稱和路由值重定向到指定的操作。 嘗試使用carId和Rent對象重定向Create操作。
我不知道使用多個post對象,但是您可以像這樣發布一個post對象
public class MyPostObject
{
public string carId { get; set; }
public Rent rent{ get; set; }
}
然后像這樣發布
[HttpPost]
public ActionResult Create(MyPostObject myPostObject)
{
string carId=myPostObject.carId;
Rent rent = myPostObject.rent;
....
}
更新:或者您可以將多個post對象與Ajax一起使用
$("#btnSave").on('click', function () {
var url = '@Url.Action("Create", "Rent")';
//Rent class properties
var data=
{
Brand: 'Renault',
Model: 'Megan',
};
$.ajax({
url:url,
type:"POST",
data:{
carId:'12',
rent:data
},
datatype:'json',
ContentType:'application/json;utf-8'
}).done(function(resp){
alert('Success ' +resp);
}).error(function(err){
alert("Error " + err.status);
});
});
您可以簡化您要嘗試做的事情。 需要注意的要點如下:
Create
操作,則無需鏈接到“ Rent
操作,只需直接鏈接到“ Create
操作即可。 ActionLink
還有另一個重載,可讓您指定控制器(請參見下文)。 Create
操作似乎不需要采用“ Rent rent
參數-可以在“ Create
操作內部Create
該參數,並簡化從視圖傳遞到控制器所需的數據。 請在代碼中查看我的評論以進一步說明:
視圖:
//call the Create action on the RentsController directly from the view
<div class="col-md-12">
<p>@Html.ActionLink("Rent", "Create", "Rents" , new { Id = car.Id }, null)</p>
</div>
控制器:
//modify signature to remove passing a Rent object it
//you can create this object inside of this method
//and do not need to pass one in so remove it from the method signature
[HttpPost]
public ActionResult Create(string carId)
{
if (!ModelState.IsValid)
return View();
var carToRent = context.Cars.SingleOrDefault(c => c.Id == carId);
if (carToRent == null)
return Content($"Car not found!");
var rent = new Rent(); //this line has been added since the method signature was changed
rent.Car = carToRent;
var customer = context.Customers.SingleOrDefault(c => c.UserId == User.Identity.Name);
if (customer == null)
return Content($"Customer not found!");
rent.Customer = customer;
context.Rents.Add(rent);
context.SaveChanges();
return RedirectToAction("Index");
}
最后,您可以刪除以下內容:
//delete this action entirely, if youre doing nothing other than redirecting
//to an action then just link directly to the action you want
//notice the ActionLink in the view is modified to hit the Create action directly
public ActionResult Rent(string id)
{
return RedirectToAction("Create", "Rents");
}
如果您要通過重定向返回視圖,則可能沒有傳遞參數,或者缺少以下方法
public ActionResult Create()
{
return View();
}
如注釋中所述,您將必須將必需的參數傳遞到redirect語句中。
public ActionResult Rent(string id)
{
Rent rentItem = new Rent();
return RedirectToAction("Create", "Rents", new { carId = id, rent = rentItem});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.