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.
- I am unable to use the id parameter that is passed to the expense controller
- 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);
}
}