0

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

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

Controller:

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);
        }
RawMVC
  • 123
  • 1
  • 15
  • Do the models have to be separate? Can't your view model just have a single Name and Address and then the controller decides what to do with them? If you do that, you only have to distinguish the button which was pressed which you can do like Sandip says or otherwise you can create two forms, which allows you to post directly to 2 different actions. – Lukos Sep 28 '16 at 10:50
  • Thanks for your comment. I will try to explain to you why Im using two models. The reason for that is, I want to use the client-side validation through the [Requirement]-Attribute in the ViewModel. So in case the user decides to send his order now, hes data is completely validated. I hope the reason now is clear, but if you have a new idea how this could be solved, im very interested in it, because Im still learning a lot in ASP.NET MVC :) – RawMVC Sep 28 '16 at 11:29

1 Answers1

0

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

Controller:

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

View:

@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

  • Thanks for your answer, do you have a small snippet how you would do that? – RawMVC Sep 28 '16 at 10:05
  • Thanks a lot for you explaining it and creating this code-snippet, it solved my problem completely and im very thankful for your help! :) – RawMVC Sep 28 '16 at 11:02
  • Its me again, I got one last problem, how do I check in my controller which button got hit? I tried it with the following statement: if(TempData["CallFrom"] != null && Convert.ToString(TempData["CallFrom"]) == "SenditNow") but that did not work. Do you have a tip for me? – RawMVC Sep 28 '16 at 12:20
  • I have already mention in "Note" , what ever pass in "value" you got in controller, better you put all my answer code , so you have better idea – Sandip - Frontend Developer Sep 28 '16 at 12:22
  • I will do that as soon, as my problem is solved, thats no problem for me :) Above I added the code of my controller, because I got the problem the program always jump in the else part, but not in the if part as it should, so maybe this helps to figure my problem out. – RawMVC Sep 28 '16 at 12:40
  • please add value attribute in button : – Sandip - Frontend Developer Sep 28 '16 at 13:13
  • Thanks for all of your effort, I did it like you said, but it still jumps only in the else part. I updated my sourcecode above so you can exactly see how the code in view and controller is. – RawMVC Sep 28 '16 at 13:27
  • change your button to : – Sandip - Frontend Developer Sep 28 '16 at 13:29
  • So, now everything is updated and like you suggested, but still it wont jump in the right part. I added a state property to say if its right part set 1 if it is the else part add 999 as control, and its always 999. – RawMVC Sep 28 '16 at 13:41