在适当的时间分配变量,使它们不为空

时间:2015-03-07 23:06:48

标签: jquery ajax asp.net-mvc

我想从textboxes和textarea(来自JQuery)获取html控件的值:

`

    @Html.LabelFor(model => model.subject)
    @Html.TextBoxFor(model => model.subject, new { @class = "AboutControls" })
    @Html.ValidationMessageFor(model => model.subject)
    <br /><br />
    @Html.LabelFor(model => model.from)
    @Html.TextBoxFor(model => model.from, new { @class = "AboutControls" })
    @Html.ValidationMessageFor(model => model.from)
    <br /><br />
    @Html.LabelFor(model => model.body)
    @Html.TextAreaFor(model => model.body, new { @class = "AboutControls" })
    @Html.ValidationMessageFor(model => model.body)
    <hr />
    <br /><br />
</div>

` 我可以这样做:

var subject = $("#subject").val(), from = $("#from").val(), body = $("#body").val();

但问题是当我试图获取它们时这些值为null。我应该以某种方式在点击JQuery代码中的"Send"按钮之前首先声明它们。代码如下所示:

$("#ContactDialog").dialog({
    autoOpen: true,
    hide: "fade",
    show: "bounce",
    height: $(window).height() / 2,
    width: $(window).width() / 2,
    title: "Send an email",
    buttons: [{ 
        text: "Send", click: function () {
            $.ajax({
                url: url,                    
                type: "POST",
                data: data,
                context: this,
                contentType: 'application/html; charset=utf-8',
                dataType: "json", 
                success: function () {
                    $(this).dialog("close");
                    sendConfirmMessage('msgSent');                        
                },
                error: function () {
                    sendConfirmMessage('msgNotSent');
                }
            });
        },
    }, {
        text: "Cancel", click: function () {
            $(this).dialog("close");
        }
    }]
});

如何在不为空的时间分配值?我们当然应该假设当用户点击“发送”时,它们不是空的。

为了弄清楚,我想要做的是从客户端获取值,并将它们发送到模型/服务器。如果有一种更聪明的方式,比我目前的方法,我都是耳朵

0 个答案:

没有答案