加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在回发ASP.NET MVC上获取Checkbox值

发布时间:2020-12-16 07:04:55 所属栏目:asp.Net 来源:网络整理
导读:我正在编写一个ASP.NET MVC应用程序.我是初学者,所以我确信这很容易.我引用了 ASP.NET MVC – How to get checkbox values on post,但代码不再可用,使其无用. 这是我的观点: @using (@Html.BeginForm("Promote","FPL",FormMethod.Post)){div data-role="fie
我正在编写一个ASP.NET MVC应用程序.我是初学者,所以我确信这很容易.我引用了 ASP.NET MVC – How to get checkbox values on post,但代码不再可用,使其无用.

这是我的观点:

@using (@Html.BeginForm("Promote","FPL",FormMethod.Post))
{
<div data-role="fieldcontain">
<fieldset id="PromotionInfo" name="PromotionInfo">
<legend><b>@Model.itemType Promotion Details</b></legend>
    <label for="TargetDate">Target Date: </label>
    <input type="date" id="TargetDate" data-role="datebox" data-options='{"mode": "flipbox"}' />

    <label for="EmailCC">Email cc List: </label>
    <input type="email" id="EmailCC" />

    <div  data-role="fieldcontain">
    <fieldset data-role="controlgroup">
    <legend>Choose Destination Server(s): </legend>

    @foreach (DataRow server in Model.destinationServerList.Rows)
    {
    <label for="@server.ItemArray[0]">@server.ItemArray[1]</label>  
    <input type="checkbox" name="destinationServerSID" id="@server.ItemArray[0].ToString()"  />
    }

    </fieldset>
    </div>
</fieldset>

</div>

<input type="submit" value="Submit" />
}

这是我的控制器:

public ActionResult Promote(string id)
    {
        //Model(item) construction occurs here

        return View(item);
    }

    [HttpPost]
    public ActionResult Promote(FormCollection collection)
    {
        try
        {
            string[] test = collection.GetValues("destinationServerSID");
        }
        catch (Exception ex)
        {
            return null;
        }
    }

test []变量包含一个2项数组,两个数组的值都是“on”,但是,我的项目列表超过了2个项目.对于您选择的每个值,它只包含一个“on”,但没有“off”值.我需要复选框的实际值(id字段).

解决方法

ID未发布到服务器.只有在选中复选框时才会将名称和值发布到服务器.

尝试使用@ server.ItemArray [0] .ToString()设置value属性.

<input type="checkbox" name="destinationServerSID" value="@server.ItemArray[0].ToString()"  />

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读