html.hidden for value not set in asp.net MVC core Razor view

18,449

Now I am able to answer your question why does it works for Html.Hidden but not for Html.HiddenFor.

  1. When you Html.HiddenFor with m =>m.m_newIVR.Account then it always try to set value for hidden field value whatever value available in property m.m_newIVR.Account not the value that you specify using @value = Model.AccountId.

If you want to use HiddenFor the set m_newIVR.Account in ViewModel just use following thing.

 @Html.HiddenFor(m =>m.m_newIVR.Account)
  1. Html.Hidden is not strongly type so it not depend on name. You can specify different name and value parameter. In this case It is your responsibility to generate proper name for HiddenField.

My Working Sample

Model

public class AccountDetailsViewModel
{
    public string AccountId { get; set; }
    public IVRS m_newIVR { get; set; }
}

public class IVRS
{

    [JsonProperty("_id")]
    public string Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("account")]
    public string Account { get; set; }

}

Controller Action

    [HttpGet]
    public IActionResult Index1()
    {
        AccountDetailsViewModel model = new AccountDetailsViewModel();
        //model.AccountId = "1222222";
        model.m_newIVR = new IVRS();
        model.m_newIVR.Account = "122222";
        return View(model);
    }

    [HttpPost]
    public IActionResult Index1(AccountDetailsViewModel model)
    {
        return View(model);
    }

View (Index1.cshtml)

@model WebApplication2.Controllers.AccountDetailsViewModel

@using (Html.BeginForm())
{
    @Html.HiddenFor(m =>m.m_newIVR.Account)    
    <input type="submit" />
}

// Sample Out

enter image description here

Share:
18,449
Asif Hameed
Author by

Asif Hameed

I am a Full Stack web developer with high availability. Programming is like a hobby for me and I like to do it perfectly. I’m a hard working, determined individual and easily fit in to any working environment. High-quality, in-time delivery, daily-communication &amp; forever relationship for update are guaranteed. Timezone difference doesn't matter at all. I believe in "Customer's satisfaction with quality work". When I work on a project, I work like It's my own. You don't have to worry about quality. It will be perfect. I always deliver projects on time or before deadline. I have a proven experience in: ✔ Programming skills: ASP.NET Core, ASP.NET MVC 1 - 6, C# ✔ Web Skills: Angular JS, JavaScript, AJAX, other JS libraries (Prototype.js, Knockout.js, JQuery, etc) HTML5, CSS3, Twitter Bootstrap ✔ ORM Tools: SubSonic / LINQ / Entity Framework / nHibernate and EntitySpaces ✔ Database: MS SQL Server 2005 - 2014, MySQL, RavenDB ✔ 3rd Party Controls: Telerik Kendo UI, DevExpress controls ✔ Cloud Based Services: Amazon Cloud services / Windows Azure ✔ Reporting: SSRS / Crystal Reports /.net RDLC Reports and MySQL ✔ Services: WEB API (WCF, REST) ✔ Development Methodologies: Agile, Scrum, TDD ✔ SVN, GitHub,Team Foundation Server, Microsoft Source Safe ✔ CMS: Umbraco, Orchard, Kentico, MojoPortal, AbleCommerce, NopCommerce ✔ Mobile App: Java, Object C, Xamrain, Android Studio Higher education background in IT and E-Commerce. Ability to understand project requirements easily, and define solution from abstract concept. Quick learner, adapt to new ideas quickly. Awarded several times for software and electronics projects. I have developed scalable solutions for domains like E-Commerce, Video Streaming, Web Services and API development, Payment Gateways Integration, Online Payment Processing, Social Networking, HealthCare, Insurance and Real Estate. I am very glad that you visited my profile. I looking forward to working with you soon. Best Regards, Asif Hameed

Updated on June 16, 2022

Comments

  • Asif Hameed
    Asif Hameed almost 2 years

    I am working on an asp.net MVc core application. I have a popup with a form element like this:

    @using (Html.BeginForm("AddIVR", "ITPVoice", FormMethod.Post, new { role = "form" }))
     {    
           @*@Html.HiddenFor(m =>m.m_newIVR.Account, new { @value= Model.accountID})*@
           @Html.Hidden("m.m_newIVR.Account", Model.accountID)    
     }
    

    I have a viewmodel like this:

    public class AccountDetailsViewModel
    {
        public IVRS m_newIVR { get; set; }
    }
    

    and IVRS model class like this:

     public class IVRS
     {
    
            [JsonProperty("_id")]
            public string Id { get; set; }
    
            [JsonProperty("name")]
            public string Name { get; set; }
    
            [JsonProperty("description")]
            public string Description { get; set; }
    
            [JsonProperty("account")]
            public string Account { get; set; }
    
    }
    

    I am trying to populate it in my view like this:

    @Html.HiddenFor(m =>m.m_newIVR.Account, new { @value= Model.accountID})
    

    but when i see view source, Value is null

    I tried using:

     @Html.Hidden("m.m_newIVR.Account", Model.accountID)
    

    and it shows m_newIVR.Account populated.

    Then I am posting the form to controller this action

     [HttpPost]       
            public ActionResult AddIVR(AccountDetailsViewModel model)
    {
     return RedirectToAction("AccountDetails", "mycontroller")
    }
    

    Although I see that AccountId is populated in view ( using viewsource), but in post action method value of model.m_newIVR.Account is null.

    HTML output looks like this:

            <div id="edit-ivrs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
    
    
          <div class="modal-dialog">
    <form action="/ITPVoice/AddIVR" method="post" role="form">                        <div class="modal-content">
                                <div class="modal-header">
    
                                    <h4 class="modal-title">Add IVR</h4>
                                    <input id="m_newIVR_Account" name="m_newIVR.Account" type="hidden" value="" />
                                    <input id="AccountId" name="AccountId" type="hidden" value="56f5e3d77ea022a042665be1" />
                                </div>
                                <div class="modal-body">
    </div>
    </div>
    

    My Questions are:

    1. Why html.hiddenfor is not setting value of the model variable?
    2. Although html.hidden is setting value, why it is not accessible in post action method ?

    Please suggest.

  • Asif Hameed
    Asif Hameed over 7 years
    I tried as @Html.HiddenFor(m =>m.m_newIVR.Account) and set the m_newIVR.Account in controller action but still its value not set. what could be the reason?
  • dotnetstep
    dotnetstep over 7 years
    Can you please provide that Action that return that view ?
  • dotnetstep
    dotnetstep over 7 years
    I have added my working sample. Make sure it will bind to Account not AccountId.