Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting text input's value from ViewBag? ASP.NET MVC5

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

like image 423
Nick Avatar asked Aug 19 '14 09:08

Nick


People also ask

How pass ViewBag value to TextBox in MVC?

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.

How do I change the ViewBag value in Cshtml?

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.

How do you set a ViewBag value?

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.

How pass data from controller view using ViewBag?

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.

What is viewbag in ASP NET MVC?

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.

How to use textboxfor instead of 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.

What is the difference between a controller and a viewbag?

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.

How to assign multiple values to viewbag property?

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.


2 Answers

Answer 1 :-

<input type="text" class="form-control" maxlength="11" id="crimeRef" 
name="crimeRef" placeholder="Crime reference" 
value="@(ViewBag.crimeRef ?? String.Empty)" >

Answer 2 :-

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!

like image 99
Kartikeya Khosla Avatar answered Sep 18 '22 10:09

Kartikeya Khosla


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!

like image 20
Nick Avatar answered Sep 18 '22 10:09

Nick