视图应在单击某些按钮时使用某些文本框和模型,该怎么做? [英] View should use certain textboxes and models on certain button clicks, how to do that?

查看:59
本文介绍了视图应在单击某些按钮时使用某些文本框和模型,该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要的是,如果用户单击两个按钮之一,则仅应使用某些文本框.目前,我对SafeForLater所做的检查始终为假,并且无法通过这种方式工作.因此,如果有人对如何使用某些文本框提出提示或建议,然后单击某个按钮并将数据绑定到我的模型,我将不胜感激.

So what I want, if a user clicks one of the two buttons only certain textboxes should be used. At the moment the check I do for SafeForLater is always false and does not work this way. So I would be thankful if someone has a hint oder advice how to use certain textboxes only then when a certain button got hit and bind the data to my model.

@model ProblemExample.Models.ViewModelOrder

@{
    ViewBag.Title = "Create Order";
}

<h2>CreateOrder</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @if (TempData["CallFrom"] != null && Convert.ToString(TempData["CallFrom"]) == "SenditNow")
        {
            @Html.TextBoxFor(m => m.viewModelSenditNow.NameOfCustomer)
            @Html.TextBoxFor(m => m.viewModelSenditNow.Address)
        }

        else
        {
            @Html.TextBoxFor(m => m.viewModelSafeForLater.NameOfCustomer)
            @Html.TextBoxFor(m => m.viewModelSafeForLater.Address)
        }
        <hr />
        <button class="formular-button-submit" type="submit" name="SenditNow" value="SenditNow">Send it now!</button>
        <button class="formular-button-submit" type="submit" name="submit" id="SafeForLater" value="SafeForLater">Save it for later!</button>
</div>
}

View中的ViewModel:

ViewModel from View:

public class ViewModelOrder
    {
        public ViewModelSendItNow viewModelSenditNow { get; set; }
        public ViewModelSafeForLater viewModelSafeForLater { get; set; }
    }

ViewModel SendItnow:

ViewModel SendItnow:

public class ViewModelSendItNow
    {
        [Required]
        public string NameOfCustomer { get; set; }
        [Required]
        public string Address { get; set; }
    }

ViewModel SaveForLater:

ViewModel SaveForLater:

public class ViewModelSafeForLater
    {
        public string NameOfCustomer { get; set; }
        public string Address { get; set; }
    }

控制器:

public ActionResult CreateOrder()
        {
            ViewModelOrder viewModel = new ViewModelOrder();
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult CreateOrder(ViewModelOrder viewModel, string submit)
        {
            TempData["CallFrom"] = submit;

            if(submit == "SendItNow")
            {
                Order myOrder = new Order()
                {
                    NameOfCustomer = viewModel.viewModelSenditNow.NameOfCustomer,
                    Address = viewModel.viewModelSenditNow.Address,
                    State = 1
                };
                db.Orders.Add(myOrder);
                db.SaveChanges();
            }

            else
            {
                Order myOrder = new Order()
                {
                    NameOfCustomer = viewModel.viewModelSafeForLater.NameOfCustomer,
                    Address = viewModel.viewModelSafeForLater.Address,
                    State = 999
                };
                db.Orders.Add(myOrder);
                db.SaveChanges();
            }
            return View(viewModel);
        }

推荐答案

您可以在控制器的按钮单击上的 Tempdata 中设置值,并检查Tempdata值而不是Request表格值.

You can set value in Tempdata on button click from controller and check that Tempdata value instead of Request.Form value.

控制器:

public ActionResult Save(string submit)
{
     TempData["CallFrom"] = submit;
     return RedirectToAction("Index");
}

查看:

@using (Html.BeginForm("Save","Home"))
{   
    <div class="form-horizontal">

        @if (TempData["CallFrom"] != null && Convert.ToString(TempData["CallFrom"]) == "SenditNow")
        {
            <h1>SenditNow</h1>
            <input type="text" value="NameOfCustomer" />
            <input type="text" value="Address" />
        }

        else
        {
            <h1>SafeForLater</h1>
            <input type="text" value="NameOfCustomer" />
            <input type="text" value="Address" />
        }
        <hr />
        <button class="formular-button-submit" type="submit" name="submit" id="SenditNow" value="SenditNow">Send it now!</button>
        <button class="formular-button-submit" type="submit" name="submit" id="SafeForLater" value="SafeForLater">Safe it for later!</button>
    </div>
}

注意:在您的控制器中获取提交"的值.仅单击的按钮会传递其值

Note: in your controller get the value of submit. Only the button clicked will pass its value

这篇关于视图应在单击某些按钮时使用某些文本框和模型,该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆