asp net mvc cross site layout

时间:2016-02-12 20:03:32

标签: asp.net-mvc razor razorengine

I have two ASP NET sites. They has different logic and pages, getting/writing data from different databases. I want to create a Master layout (with header, menu footer ect) and join this sites like they are looks like one single site. So the question is is it possible? Thanks.

1 个答案:

答案 0 :(得分:0)

You want to achieve something like displaying different models in one view? If yes, then you can use <script> function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position){ document.getElementById("xc").value = position.coords.latitude; document.getElementById("yc").value = position.coords.longitude; document.getElementById("loginform").submit(); }); } else { document.getElementById("xc").value = '0'; document.getElementById("yc").value = '0'; document.getElementById("loginform").submit(); } } </script> <form id="loginform" action="./functions/dologin.php" method="post"> <input type="hidden" id="xc" name="xc" value="X"> <input type="hidden" id="yc" name="yc" value="X"> <input class="myButton" type="button" onclick="getLocation();" value="Login"> </form> . Create ViewModels class and in here include what models do you want to display.

viewModel

Then in your action method you need to create new viewModel and pass it to the return view.

public class ViewModel
    {
        public Book Book  { get; set; }
        public IEnumerable<Book> Books { get; set; }

        public Genre Genre { get; set; }
        public IEnumerable<Genre> Genres { get; set; }
        //and so on...
    }

In your view/or layout specify the model what you are using.

    var viewModel = new ViewModel
                {
                    Books= db.Books.ToList(),
                    Genres = db.Genres.ToList()
//you can also of course specify here a Book or Genre 
                };

return View(viewModel);

And now you have access to all properties what you have specified in @model YourProjectName.ViewModels.ViewModel class.

More about View Models:

Way to Use Multiple Models in a view in ASP.NET MVC

What is ViewModel in MVC?