1

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>
4

2 回答 2

6

您不应该将整个学校对象传递给控制器​​。我将向您展示我在项目中所做的类似方法:

        [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);
        }

在我看来,我知道这是剃刀,但想法是一样的:

            <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>

为了回答你的问题,这是我的模型——它只是我在传递给视图之前填充的列表。

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

如果您的模型中没有 ID,则必须具有另一个属性,您可以通过该属性区分列表中的对象,但我建议保留某种 id。

于 2013-08-23T00:15:43.997 回答
3

像这样试试

看法

我的动作链接

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

我的控制器

 [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);
    }

我的“业务”层:

 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();
            }
        }
于 2013-08-23T05:54:42.120 回答