I am passing a variable ViewBag.crimeRef from my controller and am using it to display a message which works fine:
@if(ViewBag.crimeRef != null)
{
<div class="alert alert-dismissable alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
Sorry, there are no matching results for the crime reference <strong>@ViewBag.crimeRef</strong>. Please try searching again.
</div>
}
I figured I could use the same methodology to populate the input's value too, however I'm getting a CS1002: ; expected
error:
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference"
@if(ViewBag.crimeRef != null) { value="@ViewBag.crimeRef" }>
Is there anyway I can populate the input's value using the ViewBag variable?
Many thanks
TextBoxFor you must define a model type that defines the property that you wish to bind. Otherwise you have to use Html. TextBox and set the value manually from ViewBag . As others have said, you will make your life much easier if you use a statically typed model and take advantage of the inbuilt MVC model binding.
The ViewBag object value will be set inside Controller and then the value of the ViewBag object will be accessed in the cshtml file (View) using Razor syntax in ASP.Net MVC Razor.
You can assign a primitive or a complex type object as a value to ViewBag property. You can assign any number of properties and values to ViewBag. If you assign the same property name multiple times to ViewBag, then it will only consider last value assigned to the property.
To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.
ASP.NET MVC - ViewBag The ViewBag in ASP.NET MVC is used to transfer temporary data (which is not included in the model) from the controller to the view. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class. The following figure illustrates the ViewBag.
If you want to use TextBoxFor then you need to use strongly typed model in your view instead of ViewBag. If you want just a TextBox there are two options. Use Html.TextBox or Input type TextBox. Refer below sample.
The Controller consists of the following Action method. Inside this Action method, a string value is set into a ViewBag object i.e. Name and simply the View is returned. The View consists of a TextBox created using Html.
You can assign a primitive or a complex type object as a value to ViewBag property. You can assign any number of properties and values to ViewBag. If you assign the same property name multiple times to ViewBag, then it will only consider last value assigned to the property.
<input type="text" class="form-control" maxlength="11" id="crimeRef"
name="crimeRef" placeholder="Crime reference"
value="@(ViewBag.crimeRef ?? String.Empty)" >
I m including Nick's answer here because this is the accepted answer, so it will help other stackoverflow users to get answer of their question at one place.
Conditional statements are actually built into Razor as of MVC4 :
Conditional HTML Attributes using Razor MVC3
So simply using this:
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference"
value="@(ViewBag.crimeRef)">
Will only render the value attribute if ViewBag.crimeRef
isn't null - perfect!
Conditional statements are actually built into Razor as of MVC4: Conditional HTML Attributes using Razor MVC3
So simply using this:
<input type="text" class="form-control" maxlength="11" id="crimeRef" name="crimeRef" placeholder="Crime reference"
value="@(ViewBag.crimeRef)" }>
Will only render the value attribute if ViewBag.crimeRef
isn't null - perfect!
Thanks to Andrei for the pointer!
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