在asp.net mvc中如何将模型从partialView传递到javascript

在asp.net mvc中如何将模型从partialView传递到javascript,javascript,asp.net-mvc,razor,Javascript,Asp.net Mvc,Razor,我想将模型从partialView传递到javascript,并在controller中处理它, 现在的问题是我无法传递模型,当我运行代码时,它显示为null。有人能帮我吗 *HTML代码 @model List<TPMS.Models.Draft_SingleValue> <div class="row"> <div class="col-lg-12"> <table class="table table-bordered"&g

我想将模型从partialView传递到javascript,并在controller中处理它, 现在的问题是我无法传递模型,当我运行代码时,它显示为null。有人能帮我吗

*HTML代码

@model List<TPMS.Models.Draft_SingleValue>
<div class="row">
    <div class="col-lg-12">
        <table class="table table-bordered">
            <thead>

                <tr class="bg-gray">

                    <th>Keyword</th>
                    <th>Default Value <span class="pull-right"><i class="fa fa-edit"></i></span></th>

                    <th><span class="pull-right"><i></i></span></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var sdata in Model.OrderBy(i => i.Keyword))
                {
                    <tr id="@sdata.DraftSingleValue_ID">

                        <td id="sv:@sdata.DraftSingleValue_ID:Keyword" contenteditable="false">@sdata.Keyword</td>

                        <td id="sv:@sdata.DraftSingleValue_ID:Default_Value" contenteditable="false"> @sdata.Default_Value</td>

                        <td id="sv:@sdata.DraftSingleValue_ID" contenteditable="false" class="">
                            <span class="btn-group center-block" id="PlusButton">
                                <a class="btn btn-success btn-xs" href="javascript:AddKeyword('@sdata');"  data-id="@sdata"><i class="fa fa-plus"></i> </a>
                            </span>
                        </td>  

                    </tr>
                }
            </tbody>
            <tfoot>
                <tr class="bg-gray">

                    <th>Keyword</th>
                    <th>Default_Value <span class="pull-right"><i class="fa fa-edit"></i></span></th>

                    <th><span class="pull-right"><i></i></span></th>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
*控制器

 public ActionResult AddSingleValue(Draft_SingleValue Model)
        {

            Draft_SingleValue svmodel = new Draft_SingleValue();

            svmodel.Draft_File_ID = Model.Draft_File_ID;
            svmodel.Data_Type = Model.Data_Type;
            svmodel.Group_Name = Model.Group_Name;
            svmodel.Is_Active = Model.Is_Active;
            svmodel.Keyword = Model.Keyword;
            svmodel.Max_Length = Model.Max_Length;
            svmodel.Min_Length = Model.Min_Length;
            svmodel.Modified_By = User.Identity.Name;
            svmodel.Modified_On = DateTime.Now;
            svmodel.Remarks = Model.Remarks;
            svmodel.Default_Value = Model.Default_Value;

                _temporaryrepo.Insert_TemporarySingleValue(svmodel);


            return ListSv(svmodel.Draft_File_ID);

            //return new EmptyResult();
        }
正如你们可以从上面的代码中看到c一样,我试图将模型传递给AddKeyword函数,但我做不到。如果有人能告诉我怎么做就好了

试试这个:

查看:

@using (Html.BeginForm("YourActionMethod", "YourController", FormMethod.Post,
    new { id = "frmCreate", enctype = "multipart/form-data" }))
{       
    //code omitted for brevity
}

<script>
    $(function () {
        $('form').submit(function (event) {
            event.preventDefault();
            var formdata = new FormData($('#frmCreate').get(0));

            $.ajax({
                type: "POST",
                url: '@Url.Action("YourActionMethod", "YourController")',
                data: formdata, //! your model data
                dataType: "json",
                success: function (response) {
                    if (response.success) {
                        //success
                    }
                    else {
                        //error
                    }
                }
            });
        });
    });
</script> 
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult YourActionMethod([Bind(Exclude = null)] Model viewModel)
{
    //...
    return Json(new { success = true, message = "Success!.." }, 
        JsonRequestBehavior.AllowGet);
}

希望这有助于…

SvModel的价值是什么?你无法传递这样一个复杂的模型1)为什么(在C中)你要创建一个全新的
Draft\u SingleValue
对象(svmodel),它与请求中发送的对象相同?看起来完全没有意义,您只是创建了一个复写副本(除了修改的_By/On属性),但是您可以在原始对象上设置这些属性),没有明显的目的。2)
javascript:AddKeyword('@sdata')似乎只是传递(一些字符串表示)原始模型,而不是输入的内容。我认为,您需要序列化表单并发送它,尽管代码中并不清楚真正的意图是什么its@StephenMuecke它的内容来自数据库。e、 g(关键字=散列,数据类型=字符串,值=8589888等)不,它不是。添加
console.log({“Model”:SvModel})并检查输出以理解。即使您确实正确地序列化了它,您所做的只是将刚刚发送到视图的相同对象传递回视图(即,这将是毫无意义的)。你想用这段代码做什么?抱歉,还是不清楚。您的视图甚至不包含任何可编辑的表单控件,因此不清楚要发送到服务器的内容。最好的猜测是您希望传递
Draft\u SingleValue
模型的ID,然后根据ID获取对象并进行更新
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult YourActionMethod([Bind(Exclude = null)] Model viewModel)
{
    //...
    return Json(new { success = true, message = "Success!.." }, 
        JsonRequestBehavior.AllowGet);
}