SalarianEngineer
SalarianEngineer

Reputation: 636

Passing an object in MVC 4

I'm having a bit of trouble learning MVC 4 at the moment, and I was hoping someone could help. As far as I can tell, there isn't a duplicate of this question that showed up in search, but if there is, please direct me to it.

My question is this: in a list of schools that I have shown on my index.aspx page, is it possible to then have a user click on the "details" link such that it goes back and brings up the details for that specific school? I've included all of the relevant information from my code below, but please excuse me if it is too much (I wasn't sure exactly what was needed).

As you can see, the ActionResult Index() brings back a list of all schools in my database. What I want to do is post back when they click "details" so that the view then returns with specific information about that school. Is this possible? I figured that it would be possible to just pass the Id of the school that was clicked on, but then when I get back down to the DAC layer, it's accepting an Id (int) as a parameter, when I want it to return an object of type "School". Does this make sense?

For what it's worth, I know a small bit of Javascript, but no jQuery, JSON, etc (yet).

So I have 5 layers:

  1. Index.aspx (View)
  2. SchoolController (Controller)
  3. SchoolBus (Business layer)
  4. SchoolDAC (Database Access Class)
  5. SchoolVO (View Object)

My view object:

    SchoolVO.cs:
            public int Id { get; set; }
            public string Name { get; set; }
            public string Slogan { get; set; }

My database access class:

    SchoolDAC.cs
    public static School GetSchool(School school)
    {
        try
        {
            using (SqlConnection conn = ConnectionHelper.GetConnection("SchoolDB"))
            {
                SqlCommand cmd = new SqlCommand("Schools.GetSchool", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@SchoolId", school.Id);
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        school = readRecord(dr);
                        // readRecord() is just another method that fills the 3 properties of the school object with data from the database.
                    }
                }
                return school;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Failed to get school", ex);
        }
    }

My "business" layer:

    SchoolBus.cs:
    public static School GetSchool(School school)
    {
        school = SchoolDAC.GetSchool(school);
        return school;
    }

My Controller:

    SchoolController.cs:

    public ActionResult Index()
    {
        List<School> model = SchoolBus.GetAllSchools();
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(School school)
    {
        School model = SchoolBus.GetSchool(school);
        return View(model);
    }

My View:

    index.aspx:
    <table>
        <tr>
            <th>
                <%: Html.DisplayNameFor(model => model.Id) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Name) %>
            </th>
            <th>
                <%: Html.DisplayNameFor(model => model.Slogan) %>
            </th>
            <th></th>
        </tr>

    <% foreach (var item in Model) { %>
                <tr>
            <td>
                <%: Html.DisplayFor(modelItem => item.Id) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Name) %>
            </td>
            <td>
                <%: Html.DisplayFor(modelItem => item.Slogan) %>
            </td>
            <td>    <!--- possible to pass an object through here? --->
                <%: Html.ActionLink("Sign Up", "Index", new { id=item.Id }) %> |
                <%: Html.ActionLink("Details", "Details", new { id=item.Id }) %> |
                <%: Html.ActionLink("Delete", "Delete", new { id=item.Id }) %>
            </td>
        </tr>
    <% } %>

    </table>

Upvotes: 1

Views: 17291

Answers (2)

VsMaX
VsMaX

Reputation: 1795

You shoudln't pass whole school object to controller. I'll show you similar method that I did in my project:

        [HttpGet]
        public ActionResult Edit(int id)
        {
            DbUser editedDbUser = context.Users.Single(x => x.UserId == id);
            User editeduser = Mapper.Map<DbUser, User>(editedDbUser);
            return View("AccountInfo", editeduser);
        }

And in my View, i know it's razor but the idea is the same:

            <tr>
                <td>@(i + 1)</td>
                <td>@usersModel[i].LastName</td>
                <td>@usersModel[i].FirstName</td>
                <td>@usersModel[i].Email</td>
                <td>@Html.ActionLink("Edit", "Edit", new { id = @usersModel[i].UserId })</td>
            </tr>

To answering your question this is my model- it's just List that I am populating before passing to view.

public class Users
    {
        public List<User> UsersBag { get; set; }
    }

If you don't have an Id in your model you must have another property by which you can distinguish between objects in your list, but I recommend keeping it some kind of id.

Upvotes: 6

Jaimin
Jaimin

Reputation: 8020

Try like this,

View

My action link

 @Html.ActionLink("EditUser", "DYmanicControllerPage", "Test", new { Id = m.ID }, new { @class = "hide" })

my controller

 [HttpGet]
 public ActionResult DYmanicControllerPage(string Id)
        {
            var model = new RegisterModel();
            int _ID = 0;
            int.TryParse(Id, out _ID);
            if (_ID > 0)
            {
                RegisterModel register = GetRegisterUserById(_ID);
                model.ID = _ID;
                model.Name = register.Name;
                model.Address = register.Address;
                model.PhoneNo = register.PhoneNo;

        }
        return View(model);
    }

My "business" layer:

 public RegisterModel GetRegisterUserById(int Id)
        {
            RegisterModel model = new RegisterModel();
            using (dataDataContext _context = new dataDataContext())
            {
                return model = (from r in _context.Registrations
                                where r.RID == Id
                                select new RegisterModel
                                {

                                    ID = Id,
                                    Name = r.REName,
                                    Address = r.REAddress,
                                    PhoneNo = r.REPhoneNo
                                }).FirstOrDefault();
            }
        }

Upvotes: 3

Related Questions