MVC 4 Partial with separate controller and view

I have developed ASP.NET Forms for some time, and now I am trying to learn MVC, but this does not make full sense of how to get it to do what I want. Maybe I need to think different things. Here is what I am trying to do with a compiled example:

Purpose. Use a partial file that can be placed anywhere on the site that will accept the parameter. This parameter will be used to go to the database and return to the resulting model. Then, the view displays one or more model properties.

This is not my code, but shows what I'm trying to do.

File: Controllers /UserController.cs

[ChildActionOnly]
public ActionResult DisplayUserName(string userId)
{
MyDataContext db = new MyDataContext()

var user = (from u in db.Users where u.UserId = userId select u).FirstOrDefault();

return PartialView(user);
}

File: Views / General / _DisplayUserName.cs

@model DataLibrary.Models.User

<h2>Your username is: @Model.UserName</h2>

File: Views / About / Index.cshtml

@{
    ViewBag.Title = "About";
}

<h2>About</h2>

{Insert Statement Here}

, DisplayUserName, , userId ?

, , , , .

.

+5
2

Html.Action Html.RenderAction, :

@Html.Action("DisplayUserName", "User", new {userId = "pass_user_id_from_somewhere"});

:

[ChildActionOnly]
public ActionResult DisplayUserName(string userId)
{
    MyDataContext db = new MyDataContext()

    var user = (from u in db.Users where u.UserId = userId select u).FirstOrDefault();

    return PartialView("_DisplayUserName", user);
}

.

+6

MyDataContext... , using... , VS , ( ), dispose... , , , , . , , .

+1

All Articles