开发者

viewdata in json actionresult in mvc

开发者 https://www.devze.com 2023-02-01 16:45 出处:网络
working in mvc In this Json result i am loading some dropdownlist it is working fine..in that if i select some value description value should be displayed in the textbox....here ViewData[\"Descriptio

working in mvc

In this Json result i am loading some dropdownlist it is working fine..in that if i select some value description value should be displayed in the textbox....here ViewData["Description"] = AttributesLibrary.Description; in this i am getting description values but i cant able to display it......

 public JsonResult PopulateGradeScaleSetUp(string trackId)
    {

        List<GradeDivisionValues> GradeDivisionValuesList = new List<GradeDivisionValues>();
        Se开发者_如何学PythonlectList fieldIds = new SelectList(new List<Gradescale> { });
        AttributesLibrary AttributesLibrary = new AttributesLibrary();

        try
        {
            if (!string.IsNullOrEmpty(trackId))
            {
                string gradescaleID;
                Context.SetPrivilegeContext(GrowthDataConstant.ViewAttributeLibrary);
                AttributesLibrary = GrowthMasterDataProxy.GetAttributeByAttributeId(trackId);
                gradescaleID = AttributesLibrary.AttributePropertyDetails.GradeScaleDetails.Identifier;
                Context.SetPrivilegeContext(GrowthDataConstant.ViewGradeScale);
                Gradescale gradeScale = new Gradescale();
                gradeScale = GrowthMasterDataProxy.GetGradeScaleById(gradescaleID);
                Dictionary<string, GradeDivisionValues> GradeDivisionValuesDic = gradeScale.GradeDivisionValues;
                if (GradeDivisionValuesDic != null && GradeDivisionValuesDic.Count > 0)
                {
                    GradeDivisionValuesList = GradeDivisionValuesDic.Values.ToList();

                }
                fieldIds = new SelectList((IEnumerable)GradeDivisionValuesList, "NumericalValue", "TextValue");
                ViewData["Description"] = AttributesLibrary.Description;
            }
            return Json(fieldIds, JsonRequestBehavior.AllowGet);


        }


        catch (ArgumentException argumentException)
        {
            ExceptionService.HandleException(argumentException, _defaultPolicy);
            return Json("Error", JsonRequestBehavior.AllowGet);
        }


    }

This is my view:

 <%= Html.TextBox("Description", (string)ViewData["Description"])%>


Your controller action returns JSON which makes me to believe that you are invoking it using AJAX. In order to update the value of the textbox you need to include it in the JSON result instead of using ViewData. For example:

return Json(new
{
    fieldIds = fieldIds,
    description = AttributesLibrary.Description
}, JsonRequestBehavior.AllowGet);

Now all that's left is to use this value in your AJAX success handler. Supposing you are using jquery it might look like this:

var url = '<%= Url.Action("PopulateGradeScaleSetUp") %>';
$.getJSON(url, { trackId: 123 }, function(result) {
    $('#Description').val(result.description); 
});

and the textbox definition simply:

<%= Html.TextBox("Description") %>
0

精彩评论

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