开发者

How to set the DIV width value using the property list return value in ajax call from control page- MVC2

I am trying to set the DIV wid开发者_开发知识库th using the value return from AJAX call from the below code. When i select the check box - the javascript should trigger and sent the ajax req.Aft the ajax code execute i am return the list of property and need to use those value to set the width of the div.

            <script language="javascript" type="text/javascript">
                $(document).ready(function () {
                    $(":checkbox").click(function () {
                        var $this = $(this);
                        var checkboxData = $(':checked').val();
                         $.ajax({
                           url: '/Home/About',
                           type: 'POST',
                           data: { id: checkboxData },
                           success: function (result) {
        //I am not sure that below code is correct but What am trying here to use the returned value to set the DivID and Divwidth
                           $(".#result.data(DivID)").css("width", #result.data(DivWidthList) + "px"); 
                            }       
                        });
                    });
                });

        </script>
        <%using (Html.BeginForm())
  { %>
       <%foreach (var cbName in (List<Hello_World_MVC.Option>)ViewData["Data"])
                  {%>
                  <div id="<%=cbName.OptionID%>" style="background-color:Blue;">
                   <input type="checkbox" id="myCheckbox" class="<%=cbName.OptionID%>" value="<%=cbName.OptionID%>1" /><%=cbName.OptionName%>
                  </div>
                <%} %>  
   <%} %>  

Model Page:

public class ValueProperty
{
    private List<double> divWidthList = new List<double>();
     public List<double> DivWidthList { get; set; }
    public string DivID { get; set; }
}

Control page:

    public ActionResult About(int id)
    {
        AboutModels ObjAM = new AboutModels();//model class name
        ViewData["Data"] = ObjAM.dbValue();
        ValueProperty ObjVP = new ValueProperty();
        for (int i = 20; i <= 100; i += 20)
        {
            ObjVP.DivWidthList.Add(i);//adding the value to property list
        }
        foreach (var DivValue in ObjAM.dbValue())
        {
            ObjVP.DivID = Convert.ToString(DivValue.OptionID);//insert the value for DivID
        }
        ObjAM.dbUpdate(id);
        return Json(new { data = ObjVP});//return the property value
    }

I need to use the returned ajax value for set the DivID and DivWidth.Here i have included my Model,Control and View page. Please advice


In the AJAX success callback the result variable will be the JSON serialized model that you returned in the controller action. Now the problem with your model is that it contains the ID but it also contains a list of widths (DivWidthList) and I don't see the relation between those two. Also here you want to set the width of a single DOM element so you should return only its width:

return Json(new { id = ObjVP.DivID, width = ???? });

and then use it like this in the success callback:

success: function(result) {
    var id = result.id;
    var width = result.width;
    $('#' + id).css('width', width + 'px');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜