为什么我们不能做Html.ActionLink(“编辑晚餐”,“编辑”,新的{晚餐=模特})?

时间:2010-12-20 16:41:46

标签: c# asp.net-mvc

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.Dinner>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        <%:Model.Title %></h2>
    <fieldset>
        <legend>
            <%: Model.HostedBy %></legend>
        <p>
            <strong>When: </strong>
            <%: Model.EventDate.ToShortDateString() %>
            <strong>at: </strong>
            <%: Model.EventDate.ToShortTimeString() %>
        </p>
        <p>
            <strong>Description: </strong>
            <%: Model.Description %>
        </p>
    </fieldset>
    <p>
        <%: Html.ActionLink("Edit Dinner", "Edit", new { dinner=Model }) %>
        |
        <%: Html.ActionLink("Delete Dinner", "Delete", new { id=Model.DinnerId }) %>
        |
        <%: Html.ActionLink("Back to Dinner list", "Index") %>
    </p>
</asp:Content>

public ActionResult Edit(Dinner dinner)
{
    //Dinner dinner = dinnerRepository.GetDinnerById(id);

    if (dinner == null)
        return View("NotFound");
    else
        return View(dinner);
}

[HttpPost]
public ActionResult Edit(Dinner dinner, object dummy)
{
    Dinner temp = dinnerRepository.GetDinnerById(dinner.DinnerId);

    if (TryUpdateModel(temp))
    {
        dinnerRepository.Save();

        return RedirectToAction("Details", new { id = dinner.DinnerId });
    }
    else
        return View(temp);
}

3 个答案:

答案 0 :(得分:4)

您希望输出HTML看起来像什么?

最终,您在RouteValues字典中输入的任何数据都需要呈现为超链接的(文本)href的一部分: -

<a href="Dinners/Edit/<!-- We can't put a Dinner in here!! -->">Edit Dinner</a>

修改

事实上,如果你查看你的控制器代码,你就会抓住传入的模型的id并再次从数据库中查找它!

如果让action方法使用id参数冒犯你,你可以看一下使用ModelBinder框架为你做数据库查找。但这有点争议。在你对框架有所了解之前,我会先谈谈这些例子的用法。

答案 1 :(得分:2)

如果您这样做,那么整个Dinner对象及其所有属性都必须在URL中传递。我发现这比传递一个简单的ID并从中获取Dinner对象更不可取。

答案 2 :(得分:0)

或者,您可以按照Microsoft MSDN中的说明进行操作。

public class PersonController : Controller
{
    static List<Person> people = new List<Person>();

    //
    // GET: /Person/
    public ActionResult Index()
    {
        return View(people);
    }

    //
    // GET: /Person/Details/5
    public ActionResult Details(Person person)
    {
        return View(person);
    }

    //
    // GET: /Person/Create
    public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /Person/Create
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Person person)
    {
        if (!ModelState.IsValid)
        {
            return View("Create", person);
        }

        people.Add(person);

        return RedirectToAction("Index");
    }
}

<强>的Index.aspx

<h2>Index</h2>

<table>
    <tr>
        <th></th>
        <th>
            Id
        </th>
        <th>
            Name
        </th>
    </tr>

<% foreach (var person in Model) { %>

    <tr>
        <td>
            <%= Html.ActionLink("Details", "Details", person )%>
        </td>
        <td>
            <%= Html.Encode(person.Id) %>
        </td>
        <td>
            <%= Html.Encode(person.Name) %>
        </td>
    </tr>

<% } %>

</table>

<p>
    <%= Html.ActionLink("Create New", "Create") %>
</p>

<强> Create.aspx

<h2>Create</h2>

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Id">Id:</label>
            <%= Html.TextBox("Id") %>
            <%= Html.ValidationMessage("Id", "*") %>
        </p>
        <p>
            <label for="Name">Name:</label>
            <%= Html.TextBox("Name") %>
            <%= Html.ValidationMessage("Name", "*") %>
        </p>
        <p>
            <label for="Age">Age:</label>
            <%= Html.TextBox("Age") %>
            <%= Html.ValidationMessage("Age", "*") %>
        </p>
        <p>
            <label for="Street">Street:</label>
            <%= Html.TextBox("Street") %>
            <%= Html.ValidationMessage("Street", "*") %>
        </p>
        <p>
            <label for="City">City:</label>
            <%= Html.TextBox("City") %>
            <%= Html.ValidationMessage("City", "*") %>
        </p>
        <p>
            <label for="State">State:</label>
            <%= Html.TextBox("State") %>
            <%= Html.ValidationMessage("State", "*") %>
        </p>
        <p>
            <label for="Zipcode">Zipcode:</label>
            <%= Html.TextBox("Zipcode") %>
            <%= Html.ValidationMessage("Zipcode", "*") %>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

<div>
    <%=Html.ActionLink("Back to List", "Index") %>
</div>

<强> Details.aspx

<h2>Details</h2>

<fieldset>
    <legend>Fields</legend>
    <p>
        Id:
        <%= Html.Encode(Model.Id) %>
    </p>
    <p>
        Name:
        <%= Html.Encode(Model.Name) %>
    </p>
    <p>
        Age:
        <%= Html.Encode(Model.Age) %>
    </p>
    <p>
        Street:
        <%= Html.Encode(Model.Street) %>
    </p>
    <p>
        City:
        <%= Html.Encode(Model.City) %>
    </p>
    <p>
        State:
        <%= Html.Encode(Model.State) %>
    </p>
    <p>
        Zipcode:
        <%= Html.Encode(Model.Zipcode) %>
    </p>
</fieldset>
<p>
    <%=Html.ActionLink("Back to List", "Index") %>
</p>