简体   繁体   English

对部分视图的Ajax调用不绑定MVC3

[英]Ajax call to partial view dont bind MVC3

I choose from dropdown menu an item and click add => ajax call a method which return JsonResult this is all ok. 我从下拉菜单中选择一个项目,然后单击添加=> ajax调用返回JsonResult的方法,一切正常。 Then this data should be send to another function PartialViewResult on server side: public PartialViewResult _SkupinaRow(skupinaRow skupinaRow), which generate a new tr with some textbox and labels. 然后,应将此数据发送到服务器端的另一个函数PartialViewResult:public PartialViewResult _SkupinaRow(skupinaRow skupinaRow),该函数将生成带有一些文本框和标签的新tr。 My problem is that no binding is made. 我的问题是没有绑定。 I get Null when debuggin in _SkupinaRow(skupinaRow skupinaRow) 当在_SkupinaRow(skupinaRow skupinaRow)中调试时,我得到Null

I have the following domain model defined: 我定义了以下域模型:

  public class skupinaRow
{

   public BONUSMALUS bonusmalus { get; set; } //items 
   public KOLEDAR koledar { get; set; } //calendar

}

Partial View: 部分视图:

 @model ObracunPlac.ViewModel.skupinaRow

 @Html.HiddenFor(x => x.bonusmalus.bon_id)

..... .....

Partial view code: 部分视图代码:

  public PartialViewResult _SkupinaRow(skupinaRow skupinaRow)
    {
        return PartialView("_SkupinaRow", skupinaRow);
    }

Ajax Call: Ajax电话:

    $("#addItemPrihodki").live("click", function () {

    var id = $("#prihodkidodaj option:selected").val()

    var skupinaRow = {
        bonusmalus:{},
        koledar:{}
    }

    jQuery.getJSON("/Placa/getBonusMalus/?id=" + id, function (data) {
    console.log("JSON Data: " + data.koledar.kol_id);

        skupinaRow.koledar.kol_id = data.koledar.kol_id,  //ok

        skupinaRow.bonusmalus.bon_id = data.bonusmalus.bon_id,  //ok




        //alert(JSON.stringify(GetBonusMalusModel($("#bonusdodaj option:selected").val())));
       alert(JSON.stringify(data));
       // alert(skupinaRow.serialize());

        $.ajax({
            url: "../_skupinaRow",
            cache: false,
            data: JSON.stringify(skupinaRow),
            //data: JSON.stringify(data),
            datatype: JSON,
            success: function (html) {
                $("#editorRowPrihodki table tr#dodajNov").before(html);
                  }
               ,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
               alert('error'+"+++"+textStatus+"--- "+errorThrown);      
        },

        });

    });

    return false;
});




    public JsonResult getBonusMalus(int id)
{

    KOLEDAR koledar = db.KOLEDAR.Single(r => r.kol_id == KoledarID); 
    BONUSMALUS bm = db.BONUSMALUS.Single(r => r.bon_id == id);
    skupinaRow model = new skupinaRow
    {             
    koledar =koledar,
    bonusmalus = bm           
              };

    // return Json result using LINQ to SQL 

    return new JsonResult
    {
        JsonRequestBehavior = JsonRequestBehavior.AllowGet,
        Data = model
    };
}

Debug picture: https://www.dropbox.com/s/189q080irp0ny77/1.jpg 调试图片: https : //www.dropbox.com/s/189q080irp0ny77/1.jpg

This worked when i had one model bonusmalus but now I ned two so I created modelView. 当我有一个模型bonalmalus,但现在我需要两个模型时,此方法起作用,所以我创建了modelView。

How can I bind ViewModel-SkupinaRow to Partial View with strong type SkupinaRow ? 如何使用强类型SkupinaRow将ViewModel-SkupinaRow绑定到Partial View?

If you are using AJAX only to convert he value to json? 如果您仅使用AJAX将他的值转换为json? then you can use this approach 那么你可以使用这种方法

Set the form with normal post back to Action in controller 将带有常规过帐的表单设置回控制器中的“操作”

use jQuery in your view and on submit of form write this. 在您的视图中使用jQuery,并在提交表单时编写此代码。

$("form").submit(function(){           
            $("#DropDown_Items").val(JSON.stringify(data));
        });

Now you can use this in your Action Method. 现在,您可以在“操作方法”中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM