0

I've been banging my head against a wall about this for some time now, I don't know why this isn't working, normally I have no problems. I think I've looked at this for so long I can't figure out the answer anymore, so help would be so greatly appreciated!

I have 3 view models being loaded on one page, then using a BeginForm I submit my fields to a controller method, do some ternary expressions for some fields and RedirectToAction to a "Your form has been submitted successfully" confirmation page where I want to then display said fields they just submitted.

The view model fields come in just fine in the Submit function, but then the 2 nested view models data disappears. I clearly saw the data when it was in the controller post method and I passed the model through the RedirectToAction, but then it's just gone once I get to the verification view?

My view models:

public class MainViewModel
{
    public Guid Id { get; set; }

    public string? Name { get; set; }
    public string? Email {get; set;}

    public TypesViewModel TypeViewModel {get; set;}
}

public class TypesViewModel
{
    public Guid Id { get; set; }
    public Guid ExternalId {get; set;}

    public bool Type {get; set;}

    public string? Name { get; set; }

    public CustomFieldsViewModel CustomFieldViewModel {get; set;}
}

public class CustomFieldsViewModel 
{
    public bool Email { get; set; } 
    public bool Phone {get; set;}
    public bool Text {get; set;}

    public string? EmailContact { get; set; } 
    public string? PhoneNumberContact {get; set;}
    public string? TextMessageContact {get; set;}
    public String? PhoneNumber {get; set;}
}

My controller:

public class PrimaryController : Controller
{
    public IActionResult Index()
    {
        MainViewModel mainViewModel = new MainViewModel();

        TypesViewModel typesViewModel = new TypesViewModel();
        mainViewModel.TypeViewModel = typesViewModel;

        CustomFieldsViewModel customFieldsViewModel = new CustomFieldsViewModel();
        mainViewModel.TypeViewModel.CustomFieldViewModel = customFieldsViewModel;

        return View(mainViewModel);
    }

    [HttpPost]
    public ActionResult Submit(MainViewModel model)
    {
        model.Id = Guid.NewGuid();
        model.TypeViewModel.Id = Guid.NewGuid();
        model.TypeViewModel.ExternalId = Guid.NewGuid();
        model.TypeViewModel.Name = model.TypeViewModel.Type == true ? "Yes, Contact me." : "No, Do not contact me.";

        model.TypeViewModel.CustomFieldViewModel.EmailContact = model.TypeViewModel.CustomFieldViewModel.Email == true ? "Contact by Email" : "Do Not Contact By Email";
        model.TypeViewModel.CustomFieldViewModel.PhoneNumberContact = model.TypeViewModel.CustomFieldViewModel.Phone == true ? "Contact by Phone Number" : "Do Not Contact By Phone Number";
        model.TypeViewModel.CustomFieldViewModel.TextMessageContact = model.TypeViewModel.CustomFieldViewModel.Text == true ? "Contact by Text Message" : "Do Not Contact By Text Message";
    
        return RedirectToAction("Verification", model);
    }

    public ActionResult Verification(MainViewModel model)
    {
        return View(model);
    }
}

My views:

@model MainViewModel

@{
    ViewData["Title"] = "Primary Output";
}

<div class="container d-flex align-items-center justify-content-center">
<div class="box">
    @using (Html.BeginForm("Submit", "Primary", FormMethod.Post))
    {
        <label>Name</label>
        @Html.TextBoxFor(m => m.Name, new { @class = "form-control"})

        <label>Email Address</label>
        @Html.TextBoxFor(m => m.Email, new { @class = "form-control", onblur="checkEmail(this.value)", Type = "email"})

        <label>Can we contact you?</label>
        @Html.CheckBoxFor(m => m.TypeViewModel.Type)
        <span>Yes</span>

        <div class="contact-options">
            <label>Prefered form of communication?</label>
            <ul class="list">
                <li> <input type="checkbox" value="true" name="Model.TypeViewModel.CustomFieldViewModel.Email" id="Email"> 
                    <span>Email</span> 
                </li>
                <li> <input type="checkbox" value="true" name="Model.TypeViewModel.CustomFieldViewModel.Phone" onclick="ShowHideDiv(this)" id="Phone">
                    <span>Phone</span>
                </li>
                <li> <input type="checkbox" value="true" name="Model.TypeViewModel.CustomFieldViewModel.Text" onclick="ShowHideDiv(this)" id="Text">
                    <span>Text</span>
                </li>
            </ul>
        </div>

        <div id="Phone-Number">
            <label>Phone Number</label>
            <input class="form-control" type="tel" name="Model.TypeViewModel.CustomFieldViewModel.PhoneNumber" />
        </div>

        <div class="button-container">
            <input id="Submit" type="Submit" value="Submit" />
        </div>
    }
</div>
</div>

——

@model MainViewModel
@{
    ViewData["Title"] = "Verification";
}

<div class="container d-flex align-items-center justify-content-center">
    <div class="circular-square">
        <img src="https://i.ibb.co/YpsHjmW/The-Big-Lebowski-hp-GQ-25-Feb16-rex-b.jpg" border="0">
    </div>
</div>
<div class="verification-container d-flex align-items-center justify-content-center">
    <div class="verification-title">The Dude Abides.</div>
</div>
<div class="verification-container d-flex align-items-center justify-content-center">
    <div class="box">
        <p>Congrats! You've made it to the final round. Below is the information you submitted!</p>
        <p><label>Id:</label> @Model.Id</p>
        <p><label>Name:</label> @Model.Name</p>
        <p><label>Email:</label> @Model.Email</p>
        <p><label>Type Id:</label> @Model.TypeViewModel.Id</p>
        <p><label>Type Name:</label> @Model.TypeViewModel.Name</p>
        <p><label>Custom Field 1:</label> @Model.TypeViewModel.CustomFieldViewModel.PhoneNumberContact</p>
        <p><label>Custom Field 2:</label> @Model.TypeViewModel.CustomFieldViewModel.EmailContact</p>
        <p><label>Custom Field 3:</label> @Model.TypeViewModel.CustomFieldViewModel.TextMessageContact</p>
        @if(@Model.TypeViewModel.CustomFieldViewModel.PhoneNumber != null) {
            <p><label>Phone Number:</label> @Model.TypeViewModel.CustomFieldViewModel.PhoneNumber</p>
        }
        <p><label>External Id:</label> @Model.TypeViewModel.ExternalId</p>
     </div>
</div>
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
IAMABANANA
  • 145
  • 2
  • 18
  • `name="Model.TypeViewModel...` => `asp-for="TypeViewModel..."` or at least `name="TypeViewModel..."` MVC doesn't know what to do with a field called `Model...` – Jeremy Lakeman May 11 '23 at 06:34
  • 1
    Does this answer your question? [How do you redirect to a page using the POST verb?](https://stackoverflow.com/questions/129335/how-do-you-redirect-to-a-page-using-the-post-verb) – Sir Rufo May 11 '23 at 07:11
  • 1
    `RedirectToAction` is done by a GET request and the second argument are the route values which is converted to a dictionary and only properties with simple types are used. Your nested kind of data can only be transferred via POST method – Sir Rufo May 11 '23 at 07:18
  • I got it working finally, I think it was really a matter of Visual Studio Code not working properly. I had tried just doing a return view and then passing my model through a HttpGet to the next page with the existing model, but the page kept breaking. I tried adjusting my code again to that and now its working, thank you!! – IAMABANANA May 11 '23 at 16:37

0 Answers0