8

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

1

5 Answers 5

22

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!

Sign up to request clarification or add additional context in comments.

2 Comments

That's not valid ?? syntax.
apologies you are correct (I assumed case mattered here but forgot string is valid). I personally tend to use String when accessing static properties/methods which is why it flagged for me.
3

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!

3 Comments

great solution :) without all the null checking and all
Yeah, it's a very nice and elegant solution compared to what I attempted before!
Interesting, I wasn't aware of that.
1

The way you are currently trying to do this won't work, value isn't a server-side variable therefore you can't reference it like you are. You can, however, do something a lot simpler

value='@(ViewBag.crimeRef ?? "")'

14 Comments

@Nick clearly you trying to run this code outside the HTML then and on the server-side. Can you show me the HTML you used? My code is the exact same as the answer written by Exception only shorter - and in fact, I had to correct their answer.
@Nick no worries, your answer is better than both of ours anyway, you should mark that as the accepted one (I never realised Razor was updated to do this for you, in previous versions it would have output null).
@Exception I never said there was, I was simply saying in my opinion if this behaviour is now built into Razor then it's better to use it, clearly the OP is using a later version so in relation to their question, it's the best answer. There's nothing wrong with our answers, their just out of date :) but as you say, useful for older versions.
@Exception well if I remember correctly, your original answer was @(ViewBag.crimeRef??ViewBag.crimeRef:string.Empty) - that's not valid ?? syntax (as my comment pointed out). You posted that answer at 2014-08-19 10:00:41Z, I commented at 2014-08-19 10:03:41Z therefore your answer had been up for 3 minutes - I'd hardly call that interrupting you writing an answer. Regardless, there are no rules on how long I need to wait for commenting on an answer, if I see a wrong answer I give the user the courtesy of commenting before down voting because more often than not it's a typo.
@Exception we aren't "fighting", we are discussing :) I feel the best answer here was posted by Nick (or in actual fact, commented by Andrei). However, given Nick doesn't want the credit and has accepted your answer, my advice would be to update your answer to account for Nicks as well so other people who stumble upon this get the best possible answer.
|
1

Remove @ViewBag.crimeRef from quote and try this

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

1 Comment

The conditional operator wouldn't work here, ViewBag.crimeRef can't be evaluated as a bool - you would need to do something like String.IsNullOrEmpty(ViewBag.crimeRef) instead.
1
<input type="text" class="form-control" maxlength="11" id="crimeRef"   value="@((ViewBag.crimeRef==null)?0:ViewBag.crimeRef)" name="crimeRef" placeholder="Crime reference">

In Controller action

   ViewBag.crimeRef = null;   or        ViewBag.crimeRef = 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.