ASP MVC Foreign Key Submit Problem

I have two tables "Users" and "Costs" in the backend. UserId is a foreignKey table for expenses. I need to pass the UserId from the Usercontroller to the ExpenseController in order to save the cost information against the user ID. But there are two problems.

  • I cannot use the id parameter, which is passed to the cost controller
  • Another for creating an expense form, I could not find any userId field in the form against which I am going to save. Thus, modelstate.isvalid == false always exists.

Take a look at the following code. I hope you help me.

// UserController

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

// Inedx View (User)

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

// ExpenseController

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

// View indexes (Expense)

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

// Expense Controller (Create)

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

// Create a 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);
            }
        }
+3
1

, - , , , , . , , , Create view, , , . Create, , , FormCollection, . " " , .

[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" ) %>

    ...
<% } %>
+7
source

Source: https://habr.com/ru/post/1722856/


All Articles