在执行Ajax时将模型传回服务器

在执行Ajax时将模型传回服务器,ajax,asp.net-mvc,razor,model,Ajax,Asp.net Mvc,Razor,Model,以下是my.cshtml中的代码: @model WebComposite.Models.MyModel @using (Html.BeginForm()) { @Html.TextBoxFor(m => m.id) @Html.TextBoxFor(m => m.name) } @Scripts.Render("~/Scripts/MyScript.js") 以下是MyScript.js中的代码: $('form').submit(function (e)

以下是my.cshtml中的代码:

@model WebComposite.Models.MyModel
@using (Html.BeginForm())
{
      @Html.TextBoxFor(m => m.id)
      @Html.TextBoxFor(m => m.name)
}
@Scripts.Render("~/Scripts/MyScript.js")
以下是MyScript.js中的代码:

$('form').submit(function (e)
{
    e.preventDefault();
    //normal Ajax (my own custom version as i'm sure every one has one of these:)
    SimpleAjax("MyAction", function () { alert("Done"); });
});
以及控制器代码:

[HttpPost]
public ActionResult MyAction(MyModel model)
{
     //Problem here is model.id and model.name are empty
}
有什么想法吗?
提前感谢

快速浏览后,我想解决方案非常简单(谢谢jquery!!): 只需序列化表单中的所有内容,并从ajax调用将其发回控制器:

$("form").serializeArray();
下面是我自己对Ajax调用的完整实现 有3个函数(您需要调用SimplAjaxPost(url,funcDelegate),其余的都由它来完成

function Ajax(url, dataObj, getOrPost, theContext, theContentType, theDataType, successFuncName, failedFuncName, alwaysFuncName)
{
    //GET or POST:
    if (getOrPost == null) { getOrPost = 'POST'; }
    //Header (what we're sending to the server): http://stackoverflow.com/questions/2722750/ajax-datatype
    if (theContentType == null) { theContentType = 'application/x-www-form-urlencoded; charset=UTF-8'; }
    //response (what we're expeting in return):http://stackoverflow.com/questions/2722750/ajax-datatype
    if (theDataType == null) { theDataType = ""; }
    //exposing "this" to whatever:  http://stackoverflow.com/questions/5097191/ajax-context-option
    if (theContext == null) { theContext = document.body; }

    var theURL = NoCache(url);
    $.ajax(
    {
        url:            theURL,
        data:           dataObj,
        type:           getOrPost,
        contentType:    theContentType,
        dataType:       theDataType,
        context:        theContext,
        async:          false,
        success:        successFuncName,
        fail:           failedFuncName,
        always:         alwaysFuncName,
        complete:       AjaxScripts
    });
}

function SimpleAjax(url, successFunctionName)
{
    var dataObj = null;
    Ajax(url, dataObj, null, null, null, null, successFunctionName, null, null)
}

function SimpleAjaxPost(url, successFunctionName)
{
    var dataObj = null;
    if ($("form").length == 1)
    {
        dataObj = $("form").serializeArray();
    }
    Ajax(url, dataObj, null, null, null, null, successFunctionName, null, null)
}

function NoCache(url)
{
    var t = new Date();
    var qps = "?";
    if (url.indexOf("?") > 0)
    {
        qps = "&";
    }
    return url + qps + t.getYear() + t.getMonth() + t.getDay() + t.getHours() + t.getMinutes() + t.getSeconds() + t.getMilliseconds();
}

为什么不使用
Ajax.beginnform()
?是的,也尝试过了……似乎这里的大多数同事和海报都赞成自己做Ajax部分。总之,我找到了一个解决方法;所以我只想在下面发布它。谢谢;-)展示一下SimpleAjax方法中的内容。也许您没有在那里设置数据?