[英]How can I pass my object with RedirectToAction?
我有一个SubmitComment
Action和一个Message
类。 我想使用Message
类发送该操作是否成功完成,但是当我在ShowProduct
操作中检查它们时, Message.Result
为false且Message.Text
为null。
public class Message
{
public bool Result { get; set; }
public string Text { get; set; }
}
我的SubmitCommen
t操作:
public ActionResult SubmitComment(int productId, string comment, string nickName)
{
try
{
var productCM = new ProductCM
{
//I make my prodcut comment object here
};
storesDB.ProductCMs.Add(productCM);
storesDB.SaveChanges();
Message message = new Message
{
Result = true,
Text = "Your comment successfully registered."
};
return RedirectToAction("ShowProduct", new { id = productId, message });
}
catch (//if any exeption happend)
{
Message message = new Message
{
Result = false,
Text = "Sorry your comment is not registered."
};
return RedirectToAction("ShowProduct", new { id = productId, message });
}
}
我的ShowProduct
操作:
public ActionResult ShowProduct(int? id, message)
{
ViewBag.Message = message;
var model = storesDB.Products;
return View(model);
}
有什么问题?
您不能使用RedirectToAction
传递对象。 另外,也可以使用return ShowProduct(productId,message);
代替return RedirectToAction("ShowProduct", new { id = productId, message })
return ShowProduct(productId,message);
如下:
Message message = new Message
{
Result = true,
Text = "Your comment successfully registered."
};
return ShowProduct(productId,message);
对于RedirectToAction
,它将把参数作为查询字符串传递,而您不能传递嵌入的对象。
尝试指定类似的属性
return RedirectToAction("ShowProduct", new { id = productId, message.Result, message.Text });
为了return ShowProduct(productId,message);
,将ViewName指定为
public ActionResult ShowProduct(int? id, Message message)
{
ViewBag.Message = message;
return View("ShowProduct");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.