Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 471855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:01:40+00:00 2026-05-13T00:01:40+00:00

I have two tables Users and Expenses in the backend. UserId is a foreignKey

  • 0

I have two tables Users and Expenses in the backend. UserId is a foreignKey for the Expenses table. I need to pass the UserId from the Usercontroller to the ExpenseController to save expense info against the user id. But there are two problems.

  1. I am unable to use the id parameter that is passed to the expense controller
  2. Another is for the create expense form, i could not find any userId field in the
    the form against which i am going to save the expense. So there is always modelstate.isvalid == false.

Please look at the following code. Hope you can 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());
}

// Index view(Expense)

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

// Expense Controller(Create)

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

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

<% } %>

//Create Post

[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);
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:01:41+00:00Added an answer on May 13, 2026 at 12:01 am

    I believe that the consensus right way to do this is to construct specific models for each view and populate that model with the data necessary for the view, including data that may be necessary for any actions that the view invokes. So, for instance, you’d have an expense model that your Create view would take, one of the things it would contain would be the associated user id for the expense. The Create action that handles the post would take an expense model, instead of a FormCollection, as it’s parameter. In your Create View you would store the user id from the model in a hidden field so that it’s value gets propagated back with the post.

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

    View

    ... Inherits="System.Mvc.ViewPage<ExpenseModel>" %>
    
    <% using (Html.BeginForm()) { %>
    
        <%= Html.Hidden( "UserId" ) %>
    
        ...
    <% } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables users registered_members I want to confirm values from user table
I have two tables that need to be related--a users table and an address_book
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
using php and mysql I have two tables, a users table and a profiles
I have two tables - users and stores . there is a third table
I have two tables users table: id|name user_relationships id | user_id | friend_id and
Hi I have two tables (users & friends) I need to connect to the
I have two tables: Users and Files . Users table has 10 rows, Files
I Have two tables: Users and News. Table News has columns id, name, createdby,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.