视图应该在特定的按钮点击上使用特定的文本框和模型,如何做到这一点

本文关键字:模型 文本 这一点 何做 按钮 视图 | 更新日期: 2023-09-27 18:09:27

所以我想要的是,如果用户单击两个按钮中的一个,应该只使用某些文本框。目前,我为SafeForLater做的检查总是错误的,并且不能以这种方式工作。因此,如果有人有一个提示或建议如何使用某些文本框,那么当某个按钮被击中并将数据绑定到我的模型时,我会很感激。

@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>
}

ViewModel from View:

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

ViewModel SendItnow:

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

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值而不是请求。形成价值。

控制器:

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>
}

注意:控制器中的获取submit的值。只有被点击的按钮才会传递其值