简体   繁体   中英

How to change asp.net core view

I´m currently trying to change the view by clicking an button.

public async Task<IActionResult> OnPost(int id, [Bind("selectedTraining, selectedTimeZone, selectedLocation")] InstallTraining newTraining)
{ 

    var viewDataDictionary = new ViewDataDictionary<InstallTraining>(new EmptyModelMetadataProvider(), new ModelStateDictionary())
    {
        Model = newTraining
    };


    var viewResult = new ViewResult();
    viewResult.ViewName = "InstallTraining";
    viewResult.ViewData = viewDataDictionary;

    ViewData["InstallTraining"] = newTraining;
    ViewData["selectedLocation"] = newTraining.selectedLocation;

    this.ViewData["InstallTraining"] = newTraining;
    this.ViewData["selectedLocation"] = newTraining.selectedLocation;


    return viewResult;
}

However when I reach the new view I always get the error message that the object reference is not set to an object:

错误信息

I'm not quite sure why this happens. The view that should be shown is quite simple. When I remove the

   <div>@Model.selectedLocation</div>

it's shown but as soon as I add it I get this error.

I tried using

return View("InstallTraining", newTraining);

But I get an error that View is not known.

Does anyone have an idea what I might be doing wrong? The OnPost function is embedded in my first view that should trigger the second one.

@model didn´t work in my code. I changed it to

@Html.DisplayFor(model => model.selectedTraining)

an the code works as expected

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