0

My situation is that I have a layout page, as part of that layout page there is a main section view where certain elements on that view are called from a table/model using a foreach loop. These are references to a certain course and are called using the controller based on the URL as follows:

 public ActionResult Course_page(string id)
    {



        string abbrev = id;
        var cpage = from c in db.Course_page
                    select c;

        if (!String.IsNullOrEmpty(abbrev))
        {
            cpage = cpage.Where(x => x.abbrev.Contains(abbrev));

        }

        return View(cpage);
    }

This works fine by itself. However on the same layout there is also a login and register ajax form in a navbar and footer view both rendered by the original layout page.

So in Navbar for example there is an Ajax.Beginform and then the form itself is called currently with Html.Partial which calls the form from a page with the form HTML.

I have been trying to use solutions to my problem from the following question:

Multiple models in a view

However I haven't had much success. I did try and create a viewmodel containing all 3 models that need to be present, but I couldn't work out how to integrate that into my page given my controller above. When I referenced the viewmodel in the course page foreach loop there was always a problem and I think it was related to what I was doing in my controller or possibly the way all the pages are setup. So I've been trying the RenderPartial method in the thread above. Changing my @Html.Partial references to the login and register forms present in the navbar and footer respectively however that doesn't seem to work either. I get a 'models' do not exist in this context when I change:

@Html.Partial("_login");

to...

@{Html.RenderPartial("_login", Models.LoginViewModel);
                                        }

Bearing in mind that the LoginViewModel does exist in the relevant models namespace.

I am an absolute beginner, so it might be I am just not understanding some basic concept here. I've gone through literally every tutorial I can find and every question I can find on Viewmodels and the likes but I still can't resolve my particular problem. So I would appreciate anyone steering me in the right direction regarding my problem? Thanks.

Community
  • 1
  • 1
Rob
  • 199
  • 21
  • Rather than `Html.Partial()`, use `Html.Action()` to call a server method which returns a partial view of your login form (ditto for the Register form). But why are you including all that extra html in your layout when a user will only ever register once and only login occasionally. –  Apr 21 '16 at 11:08
  • Thanks I will try that, I'm not sure what you mean by extra html in my layout though? – Rob Apr 21 '16 at 11:16
  • Because you generating a form for Register and a form for Login in every single view (probably tripling the amount of html you send to the browser every request yet it will rarely ever be needed. If you don't want to follow the normal pattern, then at least consider loading the forms using ajax as needed. –  Apr 21 '16 at 11:20
  • @StephenMuecke Wow that actually worked perfectly and it was as simple as that. Thank you so much, I really appreciate it. – Rob Apr 21 '16 at 11:21
  • @StephenMuecke The notion behind the login/register was that they would be available on any of the 'main sites' pages, once they login they go to a different controller which would load pages not including those forms on every page in the nav and footer. Is there a better method for doing this than what I am currently doing? How could I simplify it? I will look into loading the forms with Ajax when required and thanks for the help. – Rob Apr 21 '16 at 11:29

0 Answers0