2

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.

7
  • Never set the value attribute using new { @value= Model.accountID}. Its not clear what your trying to bind to, but ir should be just @Html.HiddenFor(m =>m.m_newIVR.Account) and you set the value of Account in the controller method before you pass the model to the view. Commented Sep 15, 2016 at 1:22
  • And your Html.Hidden() does not work because it generates name="m.m_newIVR.Account" and your model does not contain a property named m Commented Sep 15, 2016 at 1:23
  • Are using Partial View ? What @model At the top of your page ? I doubt because for value you say Model.AccountId and in HiddenFor you use different model. I tried it is working for me in both case for my Model so. Commented Sep 15, 2016 at 1:59
  • my razor View has this model statement at the top @model ITPAdmin.Models.ViewModels.AccountDetailsViewModel. I have two account properties. one directly inside AccountDetailsViewModel and other in AccountDetailsViewModel.m_newIVR.Account. Commented Sep 15, 2016 at 2:10
  • I tried to set the AccountId like this model.m_newIVR.Account = model.accountID; but view before the popup opens but it is empty in the view. Commented Sep 15, 2016 at 2:10

1 Answer 1

1

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

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

3 Comments

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?
Can you please provide that Action that return that view ?
I have added my working sample. Make sure it will bind to Account not AccountId.

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.