>I can’t use the id parameter passed to the expense controller
>The other is to create an expense form, I can’t find any userId field
I want to use To save money in the form. So there is always modelstate.isvalid == false.
Please check the following code. I 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 a view
<% using (Html.BeginForm()) {%>
< fieldset>
<%= Html.TextBox("ExpenseTitle") %>
<%= Html.ValidationMessage("ExpenseTitle", "*") %>
<%= Html.TextBox("ExpenseDescription") %>
<%= Html.ValidationMessage("ExpenseDescription", "*") %>
<%= Html.TextBox("Date") %>
<%= Html.ValidationMessage("Date", "*") %>
<%= Html.TextBox("Expense") %>
<%= Html.ValidationMessage("Expense", "*") %>
p>
< %} %>
//Create a 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); }
}
[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" %>< br />
<% using (Html.BeginForm()) {%>
<%= Html.Hidden( "UserId") %>
. ..
<%} %>
I have two tables User and Expense on the backend. UserId is the foreignKey of Expenses table. I need to change UserId from Usercontroller is passed to ExpenseController to save expense information based on user ID. But there are two problems.
>I can’t use the id parameter passed to expense controller
>The other is to create expense Form, I can’t find any userId field
The form I want to use to save money. So there is always modelstate.isvalid == false.
Please check the following code. I hope you can help me.
// UserController
public ActionResult Index()
{
return View(db.Users.ToList());
}
// Inedx View(User)< /p>
<%= Html.ActionLink("Expenses", "Index", "Expense", new {id=item.Id}, null)%>
< p>// 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 a view
<% using (Html.BeginForm()) {%>
<%} %>
//Create a post
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create( FormCol lection 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);
}< br /> }
I think the correct way to reach a consensus is to build a specific model for each view and populate that model with the data required by the view, Include data that may be required for any operations called by the view. So, for example, you have a cost model that your create view will use, and one of the content contained in it will be the associated user ID of the cost. The Create operation that processes the post will use the cost Model instead of FormCollection as its parameter. In “Create View”, you can store the user ID in the model in a hidden field in order to propagate its value back to the post Sub.
[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" %>
<% using (Html.BeginForm()) {%>
<%= Html.Hidden( "UserId") %>
...
<%} %>