使用JSON/Javascript将表值返回给视图模型/控制器

Using JSON/Javascript to return table values to viewmodel/controller

本文关键字:视图 模型 返回 控制器 JSON Javascript 使用      更新时间:2023-09-26

我对Javascript/JSON比较陌生,我一直在寻找一个如何做到这一点的例子。目前我的视图中是

<p>Please enter your email and phone number registered with the account</p>
<table id="Table">
    <tr>
        <td>Email</td>
        <td> <input id = "email" type ="text" name= "email" /></td>
    </tr>
    <tr>
        <td>Phone Number</td>
        <td> <input id = "phone" type ="text" name= "phone" /></td>
    </tr>
</table>
<input type="button" value="Submit" id="sendMSG">

我想把它发送到下面的控制器

 public JsonResult SendMessage(SMSTestViewModel model)
    {
   string email = model.email;
   string number = model.phone;
    }

我想做的是写一个脚本说,当我点击按钮,发送信息,我把在两个文本框到我的视图模型,然后到我的控制器。我还想在它不刷新页面时这样做。

如果你能把我引到正确的方向,那就太好了。

我想我明白你在找什么。您想要将表单发送到SendMessage操作结果?

这就是你想要的。

function submit() {
    $.ajax({
        type: "POST",
        url: /SendMessage,
        contentType: "application/json",
        dataType: "json",
        data: { "email" : $("#email").value, "phone" : $("#phone").value }, 
        success: function (data) {
            console.log(data);
        },
        error: console.log("Where's the nearest bar?");
    });
}
$("#sendMSG").click(function () {
    submit();
});

对不起,我以为你在使用JQuery。

如果您不想使用JQuery,只需更改。

$("#phone") 

. getelementbyid(电话)

这里是如何在没有JQuery的情况下创建POST。

如何使AJAX调用没有jQuery?

希望这对你有帮助。