【问题标题】:ASP MVC Foreign key Submit problemASP MVC外键提交问题
【发布时间】:2011-12-01 10:58:58
【问题描述】:

我在后端有两个表用户和费用。 UserId 是 Expenses 表的外键。我需要将 UserId 从 Usercontroller 传递到 ExpenseController 以根据用户 ID 保存费用信息。但是有两个问题。

  1. 我无法使用传递给费用控制器的 id 参数
  2. 另一个是创建费用表单,我在 我要节省费用的表格。所以总会有modelstate.isvalid == false。

请看下面的代码。希望你能帮助我。

//用户控制器

public ActionResult Index()
{
    return View(db.Users.ToList());
}

// Inedx 视图(用户)

<%= Html.ActionLink("Expenses", "Index", "Expense", new { id=item.Id}, null)%>

// 费用控制器

public ActionResult Index(int id)
{
    ViewData["id"] = id;
    return View(db.Expenses.Where(x => x.Users.Id == id).ToList());
}

// 索引视图(费用)

<%= Html.ActionLink("Create New", "Create", new { id=ViewData["id"]})%>

// 费用控制器(创建)

    public ActionResult Create(int id)
    {
        //ViewData["id"] = id;
        return View();
    } 

//创建视图

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

    <fieldset>
        <legend>Fields</legend>

        <p>
            <label for="ExpenseTitle">ExpenseTitle:</label>
            <%= Html.TextBox("ExpenseTitle") %>
            <%= Html.ValidationMessage("ExpenseTitle", "*") %>
        </p>
        <p>
            <label for="ExpenseDescription">ExpenseDescription:</label>
            <%= Html.TextBox("ExpenseDescription") %>
            <%= Html.ValidationMessage("ExpenseDescription", "*") %>
        </p>
        <p>
            <label for="Date">Date:</label>
            <%= Html.TextBox("Date") %>
            <%= Html.ValidationMessage("Date", "*") %>
        </p>
        <p>
            <label for="Expense">Expense:</label>
            <%= Html.TextBox("Expense") %>
            <%= Html.ValidationMessage("Expense", "*") %>
        </p>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

<% } %>

//创建帖子

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)
    {
        var expense = new Expenses();
        try
        {
            TryUpdateModel(expense, new string[] {"UserId", "ExpenseTitle", "ExpenseDescription", "Date", "Expense" }, collection.ToValueProvider());

                if (ModelState.IsValid)
                {
                    db.AddToExpenses(expense);
                    db.SaveChanges();
                    return RedirectToAction("Index",int.Parse(collection["UserId"]));
                }
                else {
                    return View(expense);
                }


            }
            catch
            {
                return View(expense);
            }
        }

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    我相信达成共识的正确方法是为每个视图构建特定模型,并使用视图所需的数据填充该模型,包括任何可能需要的数据视图调用。因此,例如,您将拥有 Create 视图将采用的费用模型,其中包含的内容之一是费用的关联用户 ID。处理帖子的 Create 操作将采用费用模型,而不是 FormCollection,作为它的参数。在您的创建视图中,您会将模型中的用户 ID 存储在隐藏字段中,以便它的值与帖子一起传播回来。

    [AcceptVerbs( HttpVerbs.Get )]
    public ActionResult Create(int id)
    {
        return View( new ExpenseModel { UserId = id } );
    }
    
    [AcceptVerbs( HttpVerbs.Post )]
    public ActionResult Create( ExpenseModel expense )
    {
      ...
    }
    

    查看

    ... Inherits="System.Mvc.ViewPage<ExpenseModel>" %>
    
    <% using (Html.BeginForm()) { %>
    
        <%= Html.Hidden( "UserId" ) %>
    
        ...
    <% } %>
    

    【讨论】:

    • @ASP MVC——不客气。由于您是新来的,我只想指出系统的工作方式是您使用每个答案顶部的投票计数旁边的向上/向下箭头来投票您认为有帮助的答案(一旦您有足够的分数)并使用投票计数下方的复选标记接受最能回答您问题的问题。欢迎来到本站。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2011-09-07
    相关资源
    最近更新 更多