简体   繁体   中英

How to pass JsonResult to view in asp.net mvc

I have a business model class that have put some value then business model class to pass in model class properties.Now I want to pass the value in view page. But I can't use Viewbag because its required a collection.That why I can't pass value in view page.

Is it possible to pass value in view page??

If possible. how to pass it?

    public async Task<JsonResult> GetDeptName(int studentId)
    {
        var model = _scope.Resolve<FormModel>();

        if (ModelState.IsValid)
        {
            await model.DeptList(studentId);
        }

        return Json(new {model.DeptId, model.DeptName });
    }

You can pass concrete objects just like this to the View() as a parameter:

public ActionResult YourMethod()
{
    model = new YourModelObject
    {
        // Fill your properties here
    };

    return View(model);
}

In the page.cshtml you can set the model like this:

@model YourModelObject

And use like this:

@Html.LabelFor(x => x.YourProperty)
@Html.EditorFor(x => x.YourPropery)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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