I have my form with my checkbox inputs, and user can edit that. I can save these values in database good, but I don't know how to refill the form, so user can edit it.
This is my ViewBag var:
ViewBag.NewFriends = preferences.NewFriends;// this value is a boolean, false
I try to pre-set values like this:
@using (Html.BeginForm("SavePreferences", "Conta"))
{
@Html.HiddenFor(model => model.ID)
@Html.CheckBox("newFriends", new { @checked = @ViewBag.NewFriends })
@Html.Label("newFriends", "Solicitações de Amigo de Alma")
<p><input type="submit" value="Send" /></p>
}
As HTML checkbox has the value checked="checked", and not true or false, it doesn't work. @Html.CheckBox has the first parameter as input name, and second a boolean checked(true or false).
My question is how can I easily set up this value? I tried:
@Html.CheckBox("newFriends", ViewBag.NewFriends) // where ViewBag.NewFriends = false
But it doesn't work at all...
Any idea?
Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.
Cshtml is basically razor view extension and any view renders in html finally. You need to use Razor in your application as it supports server side code but raw html does not.
@ is used to switch from view markup to code.
A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.
Razor provides expression encoding to avoid malicious code and security risks. In case, if user enters a malicious script as input, razor engine encode the script and render as HTML output.
Probably extension method CheckBox
can't work with dynamic. I tried following example and it works:
@{ bool isNewFriends = ViewBag.NewFriends; }
@Html.CheckBox("newFriends", isNewFriends);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With