我想知道,有任何技术可以让我们将Model
作为RedirectToAction
例如:
public class Student{
public int Id{get;set;}
public string Name{get;set;}
}
控制器
public class StudentController : Controller
{
public ActionResult FillStudent()
{
return View();
}
[HttpPost]
public ActionResult FillStudent(Student student1)
{
return RedirectToAction("GetStudent","Student",new{student=student1});
}
public ActionResult GetStudent(Student student)
{
return View();
}
}
我的问题 - 我可以在RedirectToAction中传递学生模型吗?
答案 0 :(得分:62)
使用TempData
表示仅从一个请求持续到的一组数据 下
[HttpPost]
public ActionResult FillStudent(Student student1)
{
TempData["student"]= new Student();
return RedirectToAction("GetStudent","Student");
}
[HttpGet]
public ActionResult GetStudent(Student passedStd)
{
Student std=(Student)TempData["student"];
return View();
}
替代方式 使用查询字符串
传递数据return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});
这将生成类似Student/GetStudent?Name=John & Class=clsz
确保您要重定向到的方法以
[HttpGet]
为装饰 上面的RedirectToAction将发出带有http状态的GET请求 代码302 Found(执行url重定向的常用方法)
答案 1 :(得分:30)
只需将此操作称为redirect to action
或new
关键字即可。
[HttpPost]
public ActionResult FillStudent(Student student1)
{
return GetStudent(student1); //this will also work
}
public ActionResult GetStudent(Student student)
{
return View(student);
}
答案 2 :(得分:10)
是的,您可以使用
传递您显示的模型return RedirectToAction("GetStudent", "Student", student1 );
假设student1
是Student
将生成以下网址(假设您使用默认路由且student1
的值为ID=4
和Name="Amit"
)
.../Student/GetStudent/4?Name=Amit
RedirectToAction()
方法在内部使用模型中每个属性的RouteValueDictionary
值构建.ToString()
。但是,绑定仅在模型中的所有属性都是简单属性时才有效,如果任何属性是复杂对象或集合则失败,因为该方法不使用递归。例如,Student
包含属性List<string> Subjects
,那么该属性将导致查询字符串值为
....&Subjects=System.Collections.Generic.List'1[System.String]
并且绑定将失败,该属性将为null
答案 3 :(得分:0)
[HttpPost]
public async Task<ActionResult> Capture(string imageData)
{
if (imageData.Length > 0)
{
var imageBytes = Convert.FromBase64String(imageData);
using (var stream = new MemoryStream(imageBytes))
{
var result = (JsonResult)await IdentifyFace(stream);
var serializer = new JavaScriptSerializer();
var faceRecon = serializer.Deserialize<FaceIdentity>(serializer.Serialize(result.Data));
if (faceRecon.Success) return RedirectToAction("Index", "Auth", new { param = serializer.Serialize(result.Data) });
}
}
return Json(new { success = false, responseText = "Der opstod en fejl - Intet billede, manglede data." }, JsonRequestBehavior.AllowGet);
}
// GET: Auth
[HttpGet]
public ActionResult Index(string param)
{
var serializer = new JavaScriptSerializer();
var faceRecon = serializer.Deserialize<FaceIdentity>(param);
return View(faceRecon);
}
答案 4 :(得分:0)
[NonAction]
private ActionResult CRUD(someModel entity)
{
try
{
//you business logic here
return View(entity);
}
catch (Exception exp)
{
ModelState.AddModelError("", exp.InnerException.Message);
Response.StatusCode = 350;
return someerrohandilingactionresult(entity, actionType);
}
//Retrun appropriate message or redirect to proper action
return RedirectToAction("Index");
}
答案 5 :(得分:-3)
我确实找到了这样的东西,有助于摆脱硬编码的tempdata标签
public class AccountController : Controller
{
[HttpGet]
public ActionResult Index(IndexPresentationModel model)
{
return View(model);
}
[HttpPost]
public ActionResult Save(SaveUpdateModel model)
{
// save the information
var presentationModel = new IndexPresentationModel();
presentationModel.Message = model.Message;
return this.RedirectToAction(c => c.Index(presentationModel));
}
}