【问题标题】:How to Display Data from jsonresult on MVC4 View如何在 MVC 4 视图中显示来自 jsonresult 的数据
【发布时间】:2014-02-04 23:50:30
【问题描述】:

如何将控制器上的 jsonresult 中的数据显示到我的视图中?

请提供如何在视图上显示它的步骤。我是开发 MVC4 应用程序的新手。

这是我的操作中的代码:

public ActionResult IndexChange(string ProposalID)
    {
        PurchaseOrderViewModel po;
        int proposalId = 0;

        proposalId = Convert.ToInt32(Session["ProposalID"]);
        int ID = Convert.ToInt32(Session["InstitutionID"]);

        List<Institution> lstInstitution = new List<Institution>();

        lstInstitution = db.Institutions.Where(x => x.InstitutionID == ID).ToList();

        List<NewProposal> lstPropospal = new List<NewProposal>();
        lstPropospal = db.NewProposal.Where(x => x.ProposalID == proposalId).ToList();

        ViewBag.Proposal = new SelectList(db.NewProposal.Where(x => x.Status.StatusID == 3), "ProposalID", "ProposalCode");

        var model = new PurchaseOrderViewModel
        {
            Institution = lstInstitution,
            Proposal = lstPropospal
        };

        return Json(model, JsonRequestBehavior.AllowGet);
    }

【问题讨论】:

    标签: asp.net-mvc-4 jsonresult


    【解决方案1】:

    为什么要以 json 格式发送...直接在您的视图中发送 return View(model)

    在视图中导入您的模型@model PurchaseOrderViewModel

    【讨论】:

    • 因为我在视图上有一个下拉列表,一旦我更改了 selectedindex,我需要在文本框中填充数据。我试图返回模型,但视图不会显示我需要的数据
    • 你是通过ajax还是刷新页面?
    • 是的,我在 selectedindexchange 上使用了 ajax 并将其重定向到另一个将填充模型的操作。但是当我尝试返回模型时,View 不会显示数据。
    • url: '@Url.Action("IndexChange", "PurchaseOrder")', 类型: 'GET', 数据: { ProposalID: value }, contentType: 'application/json; charset=utf-8',成功:函数(数据){}
    • 这是我的 ajax 方法@Nilesh
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多