You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to pass a variable in view

    Hello i have a model which contain 4 property Id,Type,TextContent,Image. i have stored the value in it and i want to fetch the value of TextContent according to the Type on the view in different divs.I have tried viewbag but i am unable to fetch it. How can i access it on my view?
  • #768823
    You use a ViewModel class to transfer the data, see below snippet

    public class IndexViewModel
    public string MainNav { get; set; }
    public string SubNav { get; set; }

    public IndexViewModel(string mainnav, string subnav)
    this.MainNav = mainnav;
    this.SubNav = subnav;

    Your action method then comes out

    public ActionResult Index(string mainnav, string subnav)
    return View(new IndexViewModel(mainnav, subnav));

    for more details check out below link

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768861
    You can use this code snippet as example for How to pass a variable in view in Controller
    <a href="<?php echo base_url().'user/user_message';?>">
    <button type="submit" class="btn btn-info btn-small" title="Send Message" >Send
    <input type="hidden" name="username" value="<?php echo $username; ?>"/>

  • #768871
    Hai Sachin,
    You can use the public Property where you can set the values and then whenever you need just retrieve it. By this way, you will not loose the values which were set in the properties.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • Sign In to post your comments