C# 视图中的MVC数据传递
我正在制作我的MVC应用程序。我使用以下预定义参数打开视图:C# 视图中的MVC数据传递,c#,asp.net,.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,.net,Asp.net Mvc,Asp.net Mvc 4,我正在制作我的MVC应用程序。我使用以下预定义参数打开视图: return RedirectToAction("PickGroupForHomework", "Account", new {subject_id = id, qty=model.qty }); 这很好,数据subject\u id和qty被正确传递。但是,我的视图PickGroupforHomegraphy包含一个要填写的表单,然后进行验证。如果输入无效,窗口只需重新加载上一视图中定义的数据subject\u id和qty。我是
return RedirectToAction("PickGroupForHomework", "Account", new {subject_id = id, qty=model.qty });
这很好,数据subject\u id
和qty
被正确传递。但是,我的视图PickGroupforHomegraphy
包含一个要填写的表单,然后进行验证。如果输入无效,窗口只需重新加载上一视图中定义的数据subject\u id
和qty
。我是这样做的:
public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model)
{
ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
model.groups = entities.Groups.ToList();
model.users = entities.Users.ToList();
int id = model.subject_id;
var subj = entities.Subjects
.Where(b => b.class_id == model.subject_id)
.FirstOrDefault();
if (subj != null)
{
model.subject_name = subj.name;
}
if (ModelState.IsValid)
{
}
else
{
return View(model);
}
return View(model);
}
但是结果URL不包含我需要的数据,只是一个普通视图。如何正确操作?为了让wep应用程序正常工作,您需要两个操作,一个是为视图设置模型,另一个是实际完成发布和保存数据的工作:
public ActionResult PickGroupForHomework(int subject_id, int qty)
{
//Initialize your model here. Below is just an example.
ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
PickGroupForHomeworkViewModel model = new PickGroupForHomeworkViewModel();
model.groups = entities.Groups.ToList();
model.users = entities.Users.ToList();
model.subject_id = subject_id;
model.qty = qty;
return View("PickGroupForHomework", model);
}
[HttpPost]
public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model)
{
ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
int id = model.subject_id;
var subj = entities.Subjects
.Where(b => b.class_id == model.subject_id)
.FirstOrDefault();
if (subj != null)
{
model.subject_name = subj.name;
}
if (ModelState.IsValid)
{
//Save to database
[code goes here]
//return to a View to show your results
return View("[Your view to see the results]")
}
//Model Validation did not pass
//or exception occurred go back to View
return View(model);
}
为了让wep应用程序正常工作,您需要两个操作,一个是为视图设置模型,另一个是实际完成发布和保存数据的工作:
public ActionResult PickGroupForHomework(int subject_id, int qty)
{
//Initialize your model here. Below is just an example.
ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
PickGroupForHomeworkViewModel model = new PickGroupForHomeworkViewModel();
model.groups = entities.Groups.ToList();
model.users = entities.Users.ToList();
model.subject_id = subject_id;
model.qty = qty;
return View("PickGroupForHomework", model);
}
[HttpPost]
public ActionResult PickGroupForHomework(PickGroupForHomeworkViewModel model)
{
ClassDeclarationsDBEntities2 entities = new ClassDeclarationsDBEntities2();
int id = model.subject_id;
var subj = entities.Subjects
.Where(b => b.class_id == model.subject_id)
.FirstOrDefault();
if (subj != null)
{
model.subject_name = subj.name;
}
if (ModelState.IsValid)
{
//Save to database
[code goes here]
//return to a View to show your results
return View("[Your view to see the results]")
}
//Model Validation did not pass
//or exception occurred go back to View
return View(model);
}
为什么不使用不引人注目的验证呢。这意味着大多数问题都被客户端捕获。您是否也可以显示视图?@KwekuReginaldWade see edit您是否有此操作?public ActionResult PickGroupForHome作业()@Jasen为了正确显示视图,它必须使用预定义的
主题id
和数量打开。当通过重定向到操作
从不同的视图打开窗口时,这是一种需要,并且有效,但在执行返回(模型)
时则无效。为什么不使用不引人注目的验证呢。这意味着大多数问题都被客户端捕获。您是否也可以显示视图?@KwekuReginaldWade see edit您是否有此操作?public ActionResult PickGroupForHome作业()@Jasen为了正确显示视图,它必须使用预定义的主题id
和数量打开。当通过RedirectToAction
从不同的视图打开窗口时,它是一种需要,并且有效,但当执行返回(模型)
时,它不起作用。当我从不同的窗口打开它时,它是http://localhost:3449/Account/PickGroupForHomework?subject_id=2&qty=4
,但是,在错误的验证之后,它得到http://localhost:3449/Account/PickGroupForHomework
能否显示ActionResult PickGroupforHome作业(初始主题id,整数数量){}的操作代码没有这样的操作结果,由于数量
和主题id
是PickGroupForHomeworkViewModel
的一部分,因此当我从不同窗口打开它时,它是http://localhost:3449/Account/PickGroupForHomework?subject_id=2&qty=4
,但是,在错误的验证之后,它得到http://localhost:3449/Account/PickGroupForHomework
您可以显示操作结果PickGroupForHomework(初始主题id,整数数量){}的代码吗?没有这样的操作结果,因为qty
和主题id
是PickGroupForHomeworkViewModel
的一部分