1

I have a HomeController with this code:

public ActionResult Index()
{
    IndexModel m = new IndexModel();
    m.Test = "someString";

    m.LoginModel = new ViewLoginModel();
    m.LoginModel.Test = "FromLogin";

    return View("Login", m);
}

And in my Index.cshtml, I have this:

@model IndexModel

//bunch of HTML

@Html.Partial("_Login", Model.LoginModel)

And in the _Login.cshtml, this:

@model ViewLoginModel

//bunch of HTML

@Html.LabelFor(model => model.Test)

Basically, what I want to do is to have a separate model for a partial window (login) in my home page, and this model can be or not a child of the ViewIndexModel (ideally this shouldn't even exist). I'm trying to access the variables and write/use them in the partial view, but it doesn't work - it displays the property's name instead.

In the partial view "_Login", the intellisense also doesn't work for some reason (VS2013), although the @Html.BeginForm and stuff is working, but I can't access the model's properties.

What am I doing wrong, how do I fix this?

Also, is there a difference between these two?

@Html.Partial("~/Views/Home/_Login.cshtml")
@Html.Partial("Login")

It seems when I'm using #2 the function "Login" is being called in my HomeController, not sure why...

1
  • Html.Partial requires a function name that returns a partialView ! not a cshtml page name. Commented Oct 14, 2014 at 17:45

1 Answer 1

3

The reason it is displaying the property names is because you using an HTML helper that is designed to display the property names.

@Html.LabelFor(model => model.Test)

To simply output the value of a property in a model, you can use this syntax

@model.Test

If you want to be able to edit it (assuming its a string), then wrap it in a TextBoxFor helper

@Html.TextBoxFor(model => model.Test)

As for your second question, I would assume that without the fully qualified name of the view, that the routing engine is getting involved. I have not tested this and typically use the fully qualified name to get that extra few milliseconds of speed from .NET not having to try and guess what view I am asking for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.