Clink50
Clink50
CC#
Created by Clink50 on 9/13/2023 in #help
❔ Need help understanding Model Binding better
Okay cool that makes sense "ViewBag"
18 replies
CC#
Created by Clink50 on 9/13/2023 in #help
❔ Need help understanding Model Binding better
Thank you for your help with this! I'll try this out and see if I can get it to work 👍
18 replies
CC#
Created by Clink50 on 9/13/2023 in #help
❔ Need help understanding Model Binding better
So don't have them in the ViewModel at all, and just use the ViewBag? I always remembered that using the ViewBag was bad practice but that was a long time ago haha
18 replies
CC#
Created by Clink50 on 9/13/2023 in #help
❔ Need help understanding Model Binding better
Hmm maybe I'm asking the wrong way. The title and description and other values don't need to be in any form, they are just to display some values on the page. The values that do matter are the RedirectOption and the HoursRead. But the viewmodel requires those two properties + the 15 other static properties when I submit the form in order for all the whole thing to be bound in case of an error an I have to return the whole view back to the page. I guess I just don't understand how it's supposed to work in this case. I'm used to just sending an api request with the values that I need and then if there was an error, just showing those error messages through JS. Every example I've looked at so far is just showing a whole page as a form, but that's not realistic. I could have a page with static content that comes from the CMS displays at the top, and then a call to action form at the bottom of the page. But using MVC, I would have to have all the static content bound to the viewmodel plus the form properties in order for me to be sent back to the same page and no values changed on the form if there was an error. Hopefully I'm making sense.
18 replies
CC#
Created by Clink50 on 9/13/2023 in #help
❔ Need help understanding Model Binding better
Here is another example. Is there not a better way to do this? I shouldn't have to put all 20 values in the form as hidden elements when all I need is the redirectoption and hoursread in the form request.
@model YourNamespace.ReadingViewModel

<h1>@Model.Title</h1>
<p>@Model.Description</p>
<p>@Model.StaticValue1</p>
<p>@Model.StaticValue2</p>

@using (Html.BeginForm("Submit", "Reading", FormMethod.Post))
{
@Html.HiddenFor(m => m.Title)
@Html.HiddenFor(m => m.Description)
@Html.HiddenFor(m => m.StaticValue1)
@Html.HiddenFor(m => m.StaticValue2)
<!-- Add hidden fields for other static values -->

<div>
@Html.LabelFor(m => m.RedirectOption)
@Html.DropDownListFor(m => m.RedirectOption, new SelectList(new[] { "Google", "Amazon", "Etsy" }))
@Html.ValidationMessageFor(m => m.RedirectOption)
</div>

<div>
@Html.LabelFor(m => m.HoursRead)
@Html.TextBoxFor(m => m.HoursRead)
@Html.ValidationMessageFor(m => m.HoursRead)
</div>

<div>
<input type="submit" value="Submit" />
</div>
}
@model YourNamespace.ReadingViewModel

<h1>@Model.Title</h1>
<p>@Model.Description</p>
<p>@Model.StaticValue1</p>
<p>@Model.StaticValue2</p>

@using (Html.BeginForm("Submit", "Reading", FormMethod.Post))
{
@Html.HiddenFor(m => m.Title)
@Html.HiddenFor(m => m.Description)
@Html.HiddenFor(m => m.StaticValue1)
@Html.HiddenFor(m => m.StaticValue2)
<!-- Add hidden fields for other static values -->

<div>
@Html.LabelFor(m => m.RedirectOption)
@Html.DropDownListFor(m => m.RedirectOption, new SelectList(new[] { "Google", "Amazon", "Etsy" }))
@Html.ValidationMessageFor(m => m.RedirectOption)
</div>

<div>
@Html.LabelFor(m => m.HoursRead)
@Html.TextBoxFor(m => m.HoursRead)
@Html.ValidationMessageFor(m => m.HoursRead)
</div>

<div>
<input type="submit" value="Submit" />
</div>
}
18 replies
CC#
Created by Clink50 on 9/13/2023 in #help
❔ Need help understanding Model Binding better
Taking the below as an example, let's say I take the StudentPage as a viewmodel instead of just a Student model. The StudentPage model has a title, description, books, and 15 more properties just as an example. When I submit the form, I have to pass all 15+ properties with the form request, when all I actually needed was the name and the age in the request. Is this expected? It seems wrong.
@model MVC_BasicTutorials.Models.StudentPage

<h2>@Model.Title</h2>
<h2>@Model.Description/h2>

foreach (var book in Model.Books) {
<h3>@book.Name</h3>
<p>@book.Author</p>
...
}

// ...other 15 properties here that are used throughout the whole page

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

<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.StudentId)

<div class="form-group">
@Html.LabelFor(model => model.StudentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StudentName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StudentName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
@model MVC_BasicTutorials.Models.StudentPage

<h2>@Model.Title</h2>
<h2>@Model.Description/h2>

foreach (var book in Model.Books) {
<h3>@book.Name</h3>
<p>@book.Author</p>
...
}

// ...other 15 properties here that are used throughout the whole page

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

<div class="form-horizontal">
<h4>Student</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.StudentId)

<div class="form-group">
@Html.LabelFor(model => model.StudentName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StudentName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StudentName, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Age, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Age, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Age, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
18 replies