预填充创建视图

发布于 2024-12-19 15:54:33 字数 684 浏览 1 评论 0原文

选择帐户持有人后,我想使用所选帐户持有人的属性预填充我的订单创建视图。

到目前为止我的控制器操作:

    [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-12-26 15:54:33

不确定我是否正确理解你想在这里完成什么。我假设:

  1. 显示空创建表单
  2. 用户为 AccountHolder 提供值(自动提交发生?)
  3. 您返回预先填充的表单
  4. 最终创建步骤以将值保留到数据库

对吗?

如果是这样,请实例化您在创建表单中使用的模型/视图模型(您正在使用强类型视图,对吗?)并像这样返回它:

return View(yourobject); //Assuming the first view returned by GET request to Create action has all the properties in place

但是,只有在值丢失时才会发生这种情况,对吧。因此您可能需要向控制器添加更多逻辑来验证是否需要预填充或 db.Save()。

Not sure if I understand correctly what are you trying to accomplish here. I'm assuming :

  1. Displaying Empty Create form
  2. User provides value for AccountHolder (automatic submit happens?)
  3. You return pre-populated form
  4. Final Create step to preserve values to database

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 :

return View(yourobject); //Assuming the first view returned by GET request to Create action has all the properties in place

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.

泪意 2024-12-26 15:54:33

当您向 Order 控制器调用 RedirectToAction 时,我假设您现在位于 accountholderController 的 Create 方法中?

你到底想问什么?如果没有具体的情况,我无法给你太多帮助。

但需要注意的是:

尝试根据数据库中的 ID 而不是名称来搜索帐户持有人。您现在信任您的最终用户输入的帐户持有人姓名与数据库中输入的姓名完全相同(大小写相同,标点符号相同)。 ID 更加精确,并且需要更少的努力才能获得正确的结果。

如果您只想从列表中选择一个帐户持有人,然后打开“创建”视图,为什么要使用“创建后”方法?在您的主页(或您想放置的任何地方)上有一个包含所有帐户持有人的下拉列表会更明智。 的内容

<select name="accountholderID">
    <option value:"ID_of_accountholder">Name_of_accountholder</option>
    ...
</select>

类似于旁边添加按钮 。选择帐户持有人并单击按钮后,调用 OrderController 中的(Get,而不是 Post)Create 方法。将 accountholderID 作为参数传递。您的创建方法应该类似于:

public ActionResult Create(string accountholderID)
{
    int ID = Convert.ToInt32(accountholderID);

    ViewData["Accountholder"] = database.tbl_Accountholders.SingleorDefault(x=> x.Id == ID);

    ...

在您的创建视图中,只需访问您的帐户持有人的值,如下所示:

<% var accountholder = (accountholdertype)ViewData["Accountholder"]; %>

<span> Name is <%: accountholder.Name %> </span>

我认为这应该让您到达您想要的位置:-)

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

<select name="accountholderID">
    <option value:"ID_of_accountholder">Name_of_accountholder</option>
    ...
</select>

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:

public ActionResult Create(string accountholderID)
{
    int ID = Convert.ToInt32(accountholderID);

    ViewData["Accountholder"] = database.tbl_Accountholders.SingleorDefault(x=> x.Id == ID);

    ...

And in your Create View just access the values of your accountholder like so:

<% var accountholder = (accountholdertype)ViewData["Accountholder"]; %>

<span> Name is <%: accountholder.Name %> </span>

I think that should get you where you want to be :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文