7

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

Nick
  • 3,745
  • 20
  • 56
  • 75
  • 1
    possible duplicate of [Conditional HTML Attributes using Razor MVC3](http://stackoverflow.com/questions/8061647/conditional-html-attributes-using-razor-mvc3) – Andrei Aug 19 '14 at 10:00

5 Answers5

20

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!

Community
  • 1
  • 1
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
  • That's not valid `??` syntax. – James Aug 19 '14 at 10:03
  • 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. – James Aug 19 '14 at 10:13
2

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!

Community
  • 1
  • 1
Nick
  • 3,745
  • 20
  • 56
  • 75
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) />
Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
  • 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. – James Aug 19 '14 at 10:08
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 ?? "")'
James
  • 80,725
  • 18
  • 167
  • 237
  • Thank you, unfortunately when I try this I get a `Too many characters in character literal` error though – Nick Aug 19 '14 at 12:49
  • 1
    @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. – James Aug 19 '14 at 13:34
  • Thanks James, I'm sorry - I've just tried testing it and it runs now :) Thanks again – Nick Aug 19 '14 at 13:40
  • 1
    @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`). – James Aug 19 '14 at 14:14
  • @James..hello..there is no provision on stackoverflow to accept one's own answer although it is good answer but above answers also good because they can work in any asp. net mvc version.... – Kartikeya Khosla Aug 19 '14 at 14:24
  • 1
    @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. – James Aug 19 '14 at 14:27
  • @James...and also plz dont write fake things that you are correcting our answers as we have also done mvc a lot and small edits are allowed in first five mins of answer... thankzzzz – Kartikeya Khosla Aug 19 '14 at 14:33
  • Thank you very much again both for your help, I think James is right in that in my particular case then the built in conditional statements are the best solution, however I'm reluctant to mark my own answer as accepted, especially considering there are plenty of great answers here anyway that work with all MVC versions. – Nick Aug 19 '14 at 14:34
  • @Exception I don't write *"fake"* anything, your original answer was incorrect, I pointed this out to you in a comment and you corrected it. Regardless, I have no idea what your experience is with MVC but the issue with your answer was *C#* related, not MVC. – James Aug 20 '14 at 08:38
  • @James...well if you are saying it was wrong then plz prove it and plz don't interrupt anyone within 5 minutes of answering because at that time we just try to improve our answers.... – Kartikeya Khosla Aug 20 '14 at 08:42
  • 1
    @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. – James Aug 20 '14 at 08:49
  • @James...okk james i m sorry if i m wrong here..i don't want to fight with such a esteemed man with 40k+ reputation...sorry if i m wrong..thankzz... – Kartikeya Khosla Aug 20 '14 at 08:51
  • 1
    @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. – James Aug 20 '14 at 08:59
  • @James...yes but in my opinion that answer is already there in stackoverflow answers then repearting it here will make any difference???? – Kartikeya Khosla Aug 20 '14 at 09:00
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;
Rohith Nair
  • 1,080
  • 1
  • 17
  • 33