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

5

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

Tavel answered 19/8, 2014 at 9:57 Comment(1)
possible duplicate of Conditional HTML Attributes using Razor MVC3Annulose
F
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!

Fulks answered 19/8, 2014 at 10:0 Comment(2)
That's not valid ?? syntax.Cattail
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.Cattail
T
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!

Tavel answered 19/8, 2014 at 12:55 Comment(3)
great solution :) without all the null checking and allJudijudicable
Yeah, it's a very nice and elegant solution compared to what I attempted before!Tavel
Interesting, I wasn't aware of that.Cattail
F
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) />
Fluctuation answered 19/8, 2014 at 9:59 Comment(1)
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.Cattail
C
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 ?? "")'
Cattail answered 19/8, 2014 at 10:1 Comment(14)
Thank you, unfortunately when I try this I get a Too many characters in character literal error thoughTavel
@Tavel 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.Cattail
Thanks James, I'm sorry - I've just tried testing it and it runs now :) Thanks againTavel
@Tavel 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).Cattail
@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....Fulks
@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.Cattail
@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... thankzzzzFulks
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.Tavel
@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.Cattail
@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....Fulks
@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.Cattail
@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...Fulks
@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.Cattail
@James...yes but in my opinion that answer is already there in stackoverflow answers then repearting it here will make any difference????Fulks
J
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;
Judijudicable answered 19/8, 2014 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.