Asp.net mvc 我们可以在重定向操作中将模型作为参数传递吗?
我想知道,有什么技术可以让我们在Asp.net mvc 我们可以在重定向操作中将模型作为参数传递吗?,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,controller,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,Controller,我想知道,有什么技术可以让我们在重定向到操作中作为参数传递模型 例如: public class Student{ public int Id{get;set;} public string Name{get;set;} } 控制器 public class StudentController : Controller { public ActionResult FillStudent() { return View(); } [
重定向到操作中作为参数传递模型
例如:
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中通过学生模型吗?只需调用操作无需重定向到操作
,或者调用模型的新关键字。
[HttpPost]
public ActionResult FillStudent(Student student1)
{
return GetStudent(student1); //this will also work
}
public ActionResult GetStudent(Student student)
{
return View(student);
}
使用
表示仅从一个请求到另一个请求持续存在的一组数据
下一个
替代方法
使用查询字符串传递数据
return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});
这将生成一个GET请求,如Student/GetStudent?Name=John&Class=clsz
确保要重定向到的方法使用[HttpGet]
修饰为
上述RedirectToAction将发出带有http状态的GET请求
找到代码302(执行url重定向的常用方式)
我确实发现了这样的东西,帮助摆脱了硬编码的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));
}
}
是的,您可以通过使用
return RedirectToAction("GetStudent", "Student", student1 );
假设student1
是Student
这将生成以下url(假设您使用的默认路由和student1
的值是ID=4
和Name=“Amit”
)
../Student/GetStudent/4?Name=Amit
在内部,RedirectToAction()
方法通过使用模型中每个属性的.ToString()
值来构建一个RouteValueDictionary
。但是,只有当模型中的所有属性都是简单属性时,绑定才会起作用;如果任何属性都是复杂对象或集合,绑定就会失败,因为该方法不使用递归。例如,如果Student
包含属性List Subjects
,则该属性将导致查询字符串值为
…&Subjects=System.Collections.Generic.List'1[System.String]
绑定将失败,并且该属性将为null
[HttpPost]
[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);
}
公共异步任务捕获(字符串imageData)
{
如果(imageData.Length>0)
{
var imageBytes=Convert.FromBase64String(imageData);
使用(var流=新内存流(imageBytes))
{
var result=(JsonResult)等待识别面(流);
var serializer=新的JavaScriptSerializer();
var faceRecon=serializer.Deserialize(serializer.Serialize(result.Data));
if(faceRecon.Success)返回RedirectToAction(“Index”,“Auth”,new{param=serializer.Serialize(result.Data)});
}
}
返回Json(新的{success=false,responseText=“Der optstod en fejl-Intet billede,manglede data.”,JsonRequestBehavior.AllowGet);
}
//获取:Auth
[HttpGet]
公共操作结果索引(字符串参数)
{
var serializer=新的JavaScriptSerializer();
var faceRecon=序列化程序。反序列化(param);
返回视图(faceRecon);
}
当然,这是一个有效的电话。它不会被路由吗?我已经尝试过了,但是找不到模型值,因为路由字典处理对象,请尝试更改GetStudent
操作以接受对象,并在内部将其强制转换为Student
。另一种选择是在从FillStudent
@AndreiV传递它时使用JSON序列化它。我在FillStudent
中获得模型值。这似乎是ASP.NET MVC开箱即用支持的基本操作,但仍然不可能。我不得不求助于从我的模型中提取值来创建路由值对象,而仅仅是通过从多个参数中提取来将它们重新插入到我的模型中的GET处理程序中。什么是return GetStudent(student1)操作GetStudent必须返回带有模型student的视图“GetStudent”-为我工作-返回视图(“GetStudent”,student)这个答案不适用于我(页面从不更改),但hotfusions注释适用。请注意,虽然这个方法有效(使用@hotfusion提到的更改),但它仍然不符合PRG模式。如果用户点击F5,他们将再次复制原始请求!这将显示原始请求的url。这可能不是我想要的。我不想使用TempData
。我知道我们可以使用TempData来实现。但是我的问题是不同的,我也知道方法,但是我的问题是不同的,我能实现我的问题吗/No@AmitAgrawal,简单的否
。检查这是一个旧线程,但我想指出1)GetStudent操作在使用TempData时不需要参数2)必须注意,如果这是为了实现PRG模式,它仍然只能部分解决问题。在第二次刷新视图时,TempData将是空的(按设计)@哎呀,哈哈,呜呜?远离会话或临时数据?您所描述的问题仅在禁用ARR关联时与Azure有关。如果您需要一个有状态的解决方案(几乎所有允许您登录的web应用程序),那么TempData和会话是绝对必需的?“this.RedirectToAction”嗯,控制器有这样的方法:)我找不到RedirectToAction taht的重载,它采用lambda表达式。您确定它在System.Web.Mvc中,而不是在您正在使用的第三方库或扩展方法中吗?您能否说明如何在视图中使用faceRecon?感谢您在使用RedirectToAction时不将复杂对象、列表或集合包含到模型中的说明。这是给我修好的,我支持这个。感谢您关于复杂对象的说明。我们试图拒绝一个巨大的模型(大约200个字段),其中一个是List()。在重定向时,我们得到了大约20个从字符串到int的转换错误,它甚至没有显示错误源是什么,因为您应该提供错误处理程序。但是我不确定它是怎样的
[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);
}
[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");
}