将javascript对象传递给mvc

将javascript对象传递给mvc,javascript,jquery,ajax,asp.net-mvc,unobtrusive-ajax,Javascript,Jquery,Ajax,Asp.net Mvc,Unobtrusive Ajax,嗨,我所不知道的是这是否可能,这就是为什么我需要建议来实现这一点。问题是我想用ajax将JavaScript对象而不是模型对象发布到控制器。我像bellow一样编写代码,但找不到发布数据的方法,我知道jQueryAjax是一个选项,但在这种情况下我不想使用jQueryAjax发布数据 HTML @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } @section scripts{

嗨,我所不知道的是这是否可能,这就是为什么我需要建议来实现这一点。问题是我想用ajax将JavaScript对象而不是模型对象发布到控制器。我像bellow一样编写代码,但找不到发布数据的方法,我知道jQueryAjax是一个选项,但在这种情况下我不想使用jQueryAjax发布数据

HTML

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    <script type="text/javascript">

        var getValues=function()
        {
            //alert(1)
            var v = {
                id: $("#txtid").val(), name: $("#txtName").val()
            }
            console.log(v)
            return v;
        }

    </script>
    }

<h2>Index</h2>
@using (Ajax.BeginForm("postvalue", new AjaxOptions { HttpMethod="POST", OnBegin = "getValues" }))
{
    <input type="text" id="txtid" />
    <br />
    <input type="text" id="txtName" />
    <br />
    <button type="submit" value="Click" style="width:50px;height:50px">Click</button>
}

我想将数据发布到控制器,并在objsearchValues中捕获它们。

将JS值放在表单中的一个隐藏字段中,提交表单时,它会将值传递回控制器。

没有像jQuery.ajax()那样直接发布对象的选项。您不想使用
$.ajax()
?为什么不呢?(你认为
Ajax.BeginForm()
是什么-它使用
$.Ajax()
来发布数据,所以你已经在幕后使用了它)这就是为什么我不想再次使用它。我只想附加一个具有值的对象。可能吗?你的意思是我不想再次使用它?用什么?为什么您不只是使用
$.ajax()
?当我们在mvc中有内置功能时,我想减少使用JavaScription。真的吗?正确地生成控件(使用
name=“id”
name=“name”
及其
$.post('@Url.Action(“postvalue”),$('form').serialize(),函数(数据){…})
,即当前拥有的代码更少。
namespace AjaxBeginForm.Controllers
{
    public class AjaxBeginFormController : Controller
    {
        // GET: AjaxBeginForm
        public ActionResult Index()
        {
            return View();
        }

        public void postvalue(searchValues objsearchValues)
        {

        }
    }

    public class searchValues
    {
        public Int64 id { get; set; }
        public string name { get; set; }
    }
}