在asp.net MVC核心Razor视图中未设置html.hidden的值 [英] html.hidden for value not set in asp.net MVC core Razor view

查看:250
本文介绍了在asp.net MVC核心Razor视图中未设置html.hidden的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究asp.net MVc核心应用程序.我有一个带有这样的表单元素的弹出窗口:

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)    
 }

我有一个这样的viewmodel:

I have a viewmodel like this:

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

和IVRS模型类如下:

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})

但是当我看到视图源时,Value为空

but when i see view source, Value is null

我尝试使用:

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

它显示m_newIVR.Account已填充.

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")
}

尽管我看到在视图中(使用viewsource)填充了AccountId,但是在后处理方法中model.m_newIVR.Account的值为null.

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输出如下:

        <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>

我的问题是:

  1. 为什么html.hiddenfor没有设置模型变量的值?
  2. 尽管html.hidden是设置值,但为什么不能在后期操作方法中访问它?

请提出建议.

推荐答案

现在我可以回答您的问题,为什么它对Html.Hidden有效,但对Html.HiddenFor不起作用.

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

  1. 当您使用m => m.m_newIVR.Account使用Html.HiddenFor时,它将始终尝试为隐藏字段值设置值,而不管属性m.m_newIVR.Account中可用的值如何,而不是您使用@value = Model指定的值.帐户ID.

如果要使用Hidden,则在ViewModel中设置m_newIVR.Account即可.

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不是强类型,因此它不依赖于名称.您可以指定其他名称和值参数.在这种情况下,您有责任为HiddenField生成专有名称.

我的工作示例

模型

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; }

}

控制器操作

    [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);
    }

查看(Index1.cshtml)

View (Index1.cshtml)

@model WebApplication2.Controllers.AccountDetailsViewModel

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

//采样

这篇关于在asp.net MVC核心Razor视图中未设置html.hidden的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆