C
C#2y ago
Larsa

✅ Passing data from controller to view

Hi guys hoping for some help here. I have an eCommerce wep app using ASP.NET MVC (7.0) and I want to pass data from a controller to a view. Now I understand I can do this with the ViewBag dynamic property or strongly typed view models. The data I want pass belongs to another model entirely and not the model for which the View is for. I have a Game model which has 2 FK's Developer and Publisher - within my create() View for the Game Model I wish to have a drop down list of Developers in the database to select from. How do I achieve this? Been stuck on this step for a while now and I'm still very new to .NET. I thought I could do something like this:
<div class="form-group">
<label asp-for="DeveloperId" class="control-label"></label>
<select asp-for="DeveloperId" class="form-control" asp-items="ViewBag.Developer"></select>
<span asp-validation-for="DeveloperId" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DeveloperId" class="control-label"></label>
<select asp-for="DeveloperId" class="form-control" asp-items="ViewBag.Developer"></select>
<span asp-validation-for="DeveloperId" class="text-danger"></span>
</div>
Any help is greatly appreciated, thank you.
5 Replies
Angius
Angius2y ago
Use strongly-typed models. Always and forever. Use a DTO if needed
Larsa
Larsa2y ago
I need to do create a method in my Developer controller that can do this?
Angius
Angius2y ago
I guess you could have the model be some
class ViewModel
{
public GetData? GetData { get; set; }
public PostData? PostData { get; set; }
}
class ViewModel
{
public GetData? GetData { get; set; }
public PostData? PostData { get; set; }
}
That way the view will have only one @model You can send the data inside of the GetData property And bind the form to PostData Or you could maybe place your form inside of a partial, that would let you use a different model for it
Larsa
Larsa2y ago
Hmm ok - might have to research a little bit to figure out how to do that. Thank you for the suggestions 😊
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
How to implement that when you click on the button the data that was sent with it will be displayedHow to implement that when you click on the button the data that was sent with it will be displayed❔ Visual Studio: How to hide tptrace output from Immediate Window during Test Explorer executions?Title says it all. Is there a way to hide the output messages from tptrace that get written to the I❔ Name "PostsService" does not exist in the current context```cs @using BlazorBookGroup.Data . . . private PostsService postsService = new(); protecte❔ System.AccessViolationException: 'Attempted to read or write protected memory.Even with <AllowUnsafeCodeBlocks> enabled and using unsafe { }, this error does not go away. And bef❔ namespace 'Models' does not exist in the namespace 'BlazorBookGroup'It is a subfolder though so what's the issue?❔ .Net Core API User.FindFirstValue(ClaimTypes.NameIdentifier) in every action?!Hi everyone I have the end point below ``` [HttpPost, Route("stores/new"), Authorize(policy: "client❔ Is it possible to change a project sdk from Microsoft.NET.Sdk to Microft.net.sdk.net?I'm completely new to c# and .NET so apologies if this doesn't make sense. If a project csproj file❔ Razor pages redirectHow do you make a form redirect to a page, and pass a value to it? My entire application is routed❔ FileNotFoundException for NuGet Pkg in Az Function AppHi, I'm receiving the following error while trying to locally test an Azure Function App: ```System❔ VS 2019 Basic Start ErrorWhen I try to use even a basic code like; using System; namespace HelloWorld { class Program {