预填充创建视图
选择帐户持有人后,我想使用所选帐户持有人的属性预填充我的订单创建视图。
到目前为止我的控制器操作:
[HttpPost]
public ActionResult Create(FormCollection values)
{
var accountHolder = from a in unitOfWork.AccountHolderRepository.Get(includeProperties: "AccountHolder")
where a.CustSName == values["Name"]
select a;
foreach (var a in accountHolder)
{
ViewBag.CustFName = a.CustFName;
ViewBag.CustSName = values["Name"];
ViewBag.CustPhone = a.CustPhone;
ViewBag.CustEmail = a.CustEmail;
}
return RedirectToAction("Create", "Order");
}
After selecting an accountholder I want to prepopulate my Order Create View with the properties of the selected accountholder.
My Controller Action so far:
[HttpPost]
public ActionResult Create(FormCollection values)
{
var accountHolder = from a in unitOfWork.AccountHolderRepository.Get(includeProperties: "AccountHolder")
where a.CustSName == values["Name"]
select a;
foreach (var a in accountHolder)
{
ViewBag.CustFName = a.CustFName;
ViewBag.CustSName = values["Name"];
ViewBag.CustPhone = a.CustPhone;
ViewBag.CustEmail = a.CustEmail;
}
return RedirectToAction("Create", "Order");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我是否正确理解你想在这里完成什么。我假设:
对吗?
如果是这样,请实例化您在创建表单中使用的模型/视图模型(您正在使用强类型视图,对吗?)并像这样返回它:
但是,只有在值丢失时才会发生这种情况,对吧。因此您可能需要向控制器添加更多逻辑来验证是否需要预填充或 db.Save()。
Not sure if I understand correctly what are you trying to accomplish here. I'm assuming :
Am I right ?
If so, instantiate the model / viewmodel you're using in your create form (you're using strongly typed view right ?) and return it like this :
however that should happen only if the values are missing, right. so you might want to add some more logic to your controller to verify if pre-popullation or db.Save() is required.
当您向 Order 控制器调用 RedirectToAction 时,我假设您现在位于 accountholderController 的 Create 方法中?
你到底想问什么?如果没有具体的情况,我无法给你太多帮助。
但需要注意的是:
尝试根据数据库中的 ID 而不是名称来搜索帐户持有人。您现在信任您的最终用户输入的帐户持有人姓名与数据库中输入的姓名完全相同(大小写相同,标点符号相同)。 ID 更加精确,并且需要更少的努力才能获得正确的结果。
如果您只想从列表中选择一个帐户持有人,然后打开“创建”视图,为什么要使用“创建后”方法?在您的主页(或您想放置的任何地方)上有一个包含所有帐户持有人的下拉列表会更明智。 的内容
类似于旁边添加按钮 。选择帐户持有人并单击按钮后,调用 OrderController 中的(Get,而不是 Post)Create 方法。将 accountholderID 作为参数传递。您的创建方法应该类似于:
在您的创建视图中,只需访问您的帐户持有人的值,如下所示:
我认为这应该让您到达您想要的位置:-)
As you're calling RedirectToAction to the Order controller, I assume you are now in the Create method of the accountholderController?
What's your question exactly? Without specifics, I can't give you much help.
Some notes though:
Try to search for the accountholder based on its ID in the database rather than the name. You are now trusting your enduser to enter the accountholder name exactly as it is entered in the database (same case, same punctuation). ID's are more precise and require less effort to get right.
Why use the Post-Create method if all you want to do is select an accountholder from a list and then open a Create view? It would be much wiser to have a dropdownlist containing all accountholders on your main page (or wherever you want to put it). something along the lines of
Add a button next to that. Once an accountholder is selected and the button is clicked, call your (Get, not Post) Create method in the OrderController. Pass the accountholderID as a parameter. Your Create methoud should be something like:
And in your Create View just access the values of your accountholder like so:
I think that should get you where you want to be :-)