controller action returning View(tuple) asp.net mvc 3 - asp.net-mvc-3

Is there a way for the controller to pass a tuple as a model to the View?
Something like this:
return View(Tuple<LandingPage, Models.FilterModel>(landingPage, filter));
Thank you

Yes, you just need to instantiate the Tuple:
return View(new Tuple<LandingPage, Models.FilterModel>(landingPage, filter));
Although you're probably better off creating a composite view model type which contains properties for the landing page and the filter model.

Related

Asp.net Core MVC ModelMetadata.AdditionalValues add function

I am upgrading MVC5 to Asp.Net Core 3 MVC and just found that ModelMetadata.AdditionalValues has been changed to IReadOnlyDictionary. We have stored information in additionalValues before. What is the alternative solution now?
Thanks in advance.
Wilson
What is the alternative solution now?
You could use ViewData and ViewBag to pass data to a view in ASP.NET Core.
ViewData
is a property of the Controller Base class, which returns a ViewDataDictionary object.
The ViewDataDictionary is a dictionary object which allows us to store key-value pairs. The key must be a case-insensitive string. To pass data to the view, you can assign values to the dictionary using the Key. You can store any number of items as needed in the ViewData.
The data stored in the ViewData object exists only during the current request. As soon as the view is generated and sent to the client, the Viewdata object is cleared.
The following is an example that sets values for a Title using ViewData in an action:
1. Codes in Controller
public IActionResult Index()
{
ViewData["Title"] = "Hello World!";
return View();
}
2. Codes in View
#ViewData["Greeting"]
ViewBag
isn't available in Razor Pages. ViewBag is a DynamicViewData object that provides dynamic access to the objects stored in ViewData. ViewBag can be more convenient to work with, since it doesn't require casting. The following example shows how to use ViewBag with the same result as using ViewData above:
1. Codes in Controller
public IActionResult SomeAction()
{
ViewBag.Greeting = "Hello";
return View();
}
2. Codes in View
#ViewBag.Greeting World!
Here you can find more information like below in this link.
ViewData attribute - Another approach that uses the ViewDataDictionary is ViewDataAttribute.
Using ViewData and ViewBag simultaneously
Summary of the differences between ViewData and ViewBag
When to use ViewData or ViewBag
Strongly typed data (ViewModel)

Voyager dropdown relationship ajax call

How i can override model query which populate a belongsto relationship dropdown via ajax call.
Tha call is like http://santinisacri.local/admin/santini-soggetti/relation?type=santini_soggetti_hasone_santini_soggetti_relationship&method=add&page=1 and the response is a JSON.
I want to add a where clausole.
I have tried in model but nothing. I need to create a new controller?
I resolved by a overriding controller and return an array like:
$results['results'] = $soggetti;
return $results;

In partial view: "The model item passed into the dictionary is of type"

I lack understanding of some basic MVC concepts, despite all my searching.
I created an MVC project in Visual Studio, which contains the partial view _LogOnPartial.shtml. I just want to access information within the view pertaining to the user, to put in a user dropdown menu. When I try to put this at the top of the partial view cshtml page I get the above error:
#model MyProject_MVC.Models.UserRepository
When I try this I also get an error:
#Html.Partial("_LogOnPartial", MyProject_MVC.Models.UserRepository)
'MyProject_MVC.Models.UserRepository' is a 'type', which is not valid in the given context
You have to provide an instance of MyProject_MVC.Models.UserRepository to the partial view. _LogOnPartial is strongly-typed to that type. It lets you access its public members at compile time and decide how you can display it in your view.
If you want to use your own type in that view, first you have to change the type that is strongly-typed to it.
#model MyProject_MVC.Models.UserRepository
Then you have to create an instance of that type in your action method and pass it to the view as the model or a property of the model.
public ActionResult LogOn()
{
return View(new UserRepository());
}
Now you can access this instance as Model object in your view.
#Html.Partial("_LogOnPartial", Model);
#model _LogOnPartial.Models.UserRepository should probably be: #model MyProject_MVC.Models.UserRepository
and for the last part, you have to provide and instance of the type UserRepository as the second parameter, not the type itself.

How can i supply value to Textbox generate from #Html.EditorFor in MVC razor?

I am just new to MVC.
when we use "#Html.EditorFor" in razor view, it generates textbox.
My requirement is that I need to supply some value from viewbag or session to user's in that textbox?
Is it possible and if yes how can i do?
OR
What are the alternatives?
In your action method in the controller, pre-load a model with some data:
public ActionResult Index()
{
MyModel model = new MyModel();
model.FirstName = "Bob";
model.LastName = "Hoskins";
return View(model);
}
Then make your View strongly typed. These pre-set values should now appear on your view. You probably want to populate them from a service layer or resource file, rather than have them as hardcoded strings like my example.

MVC 3 passing entity as an Interface

I'm currently working on an MVC 3 project using Ninject as my DI, the business objects are stored in a separate assembly. I'm running into an issue with the controller parameters, when posting back for CRUD operations I'm getting the error "Cannot create an instance of an interface". I am aware that you can't create an instance of an interface, but it seems like the only way I can get around this is to use a custom model binder and pass the FormCollection through. This seems really messy and I want to keep as much type specific code out of the project as I can - hence interfaces everywhere and Ninject to DI the concretes. Not only does custom model binding seem messy - won't I also lose my DataAnnotations?
Some code to describe what I have:
public ActionResult Create()
{
// I'm thinking of using a factory pattern for this part
var objectToCreate = new ConcereteType();
return (objectToEdit);
}
[HttpPost]
public ActionResult Create(IRecord record)
{
// check model and pass to repository
if (ModelState.IsValue)
{
_repository.Create(record);
return View();
}
return View(record);
}
Has anyone run into this before? How did you get over it?
Thanks!
but it seems like the only way I can get around this is to use a custom model binder
A custom model binder is the correct way to go. And by the way you should use view models as action arguments, not domain models or interfaces.
Not only does custom model binding seem messy - won't I also lose my DataAnnotations?
I don't know why you think that a custom model binder would make things messy. For me it's a great way to separate mapping logic into a reusable class. And, no you will not lose DataAnnotations. They will work perfectly fine on the concrete instance that the custom model binder would return.
Data passed to controllers action are simply holders for values. There shouldn't be any logic in them so there is nothing to decouple from. You can use concrete types (e.g Record) instead of interface (IRecord)
I made the same simple mistake. Ninject injects parameters into your constructor, but you added parameters to the Index Controller action.
It should look like this:
public class HomeController : Controller
{
private IRecord _record;
public HomeController(IRecord record)
{
_record = record;
}
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application. " +
_record .HelloWorld();
return View();
}
}
Make sense?

Resources