【问题标题】:JSON click event empty parameterJSON点击事件空参数
【发布时间】:2013-03-24 10:17:45
【问题描述】:

点击按钮时参数string Temat为空 哪里有问题?看起来还可以吗?

文件.cshtml

    @using (Html.BeginForm())
    {
    <table class="edt" style="float: left; width: 600px; margin-top: 5px; background-color: white;">
    <tr>
                <td class="labelm">
                    @Html.LabelFor(m => m.Temat)<span id="Temat_2" class="a">*</span>
                </td>
                <td>
                    @Html.TextBoxFor(m => m.Temat,  new { id = "Temat" })
                </td>
    </tr>
    </table>
    <input type="button" value="save" onclick="Save();" />
    }

<script type="text/javascript">

    function Save() {
    var Temat = $('#Temat').val();

            $.ajax({
                url: '@Url.Action("DodajTematSave", "StronaGlowna")',
                dataType: "json",
                data: { Temat: Temat },
                type: "POST",
                async: false,
                error: function() {
                },
                success: function(data) {
                    if (data.Success) {
                        alert('success');
                    }

                }
            });
        }

</script>

控制器中的 JSON 方法:

public JsonResult DodajTematSave(string Temat)
{
    var model = new StronaGlowna();
    if (!TryUpdateModel(model) || !ModelState.IsValid) // podładowanie yield 
    {
    }
    string chceSprobowac = Temat;
    return Json(new { Success = true});
}

【问题讨论】:

    标签: c# json asp.net-mvc-3


    【解决方案1】:

    我想你想检索文本框的值。如果是这样,请将 id 'Temat' 分配给文本框而不是 span,您的代码应该可以正常工作。

    但如果您有兴趣检索跨度的文本,请使用

    var Temat = $('#Temat').text();
    

    如果要为文本框分配 id,

    @Html.TextBoxFor(model => model.Password, new { id = "Temat" })
    

    【讨论】:

    • 我在测试时遇到了问题,因为 @Html.LabelFor(m => m.Temat) 和 @Html.TextBoxFor(m => m.Temat, new { size = 47 }) 有类似的 id 我应该如何为其中一个更改它?
    • 文本框中的值是空的,但我不知道为什么在我得到“*”之前这是来自 span 的文本,但我需要来自文本框的文本。
    • 只需将 id 'Temat' 分配给文本框并将其从跨度中删除。您的代码应该可以正常工作。
    • 我查了一下,它s not work. I dont 知道为什么。我无法从文本框获取数据
    • 代码现在看起来不错。能否从浏览器查看生成的html,看看id是否设置正确?
    猜你喜欢
    • 2017-09-18
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多