ASP.NET MVC 2 - Saving child entities on form submit - asp.net

I'm using ASP.NET MVC 2 and am struggling with saving child entities. I have an existing Invoice entity (which I create on a separate form) and then I have a LogHours view that I'd like to use to save InvoiceLog's, which are child entities of Invoice. Here's the view:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TothSolutions.Data.Invoice>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Log Hours
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("#InvoiceLogs_0__Description").focus();
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Log Hours</h2>
<% using (Html.BeginForm("SaveHours", "Invoices")) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<table>
<tr>
<th>Date</th>
<th>Description</th>
<th>Hours</th>
</tr>
<%
int index = 0;
foreach (var log in Model.InvoiceLogs) {
%>
<tr>
<td><%: log.LogDate.ToShortDateString() %></td>
<td><%: Html.TextBox("InvoiceLogs[" + index + "].Description")%></td>
<td><%: Html.TextBox("InvoiceLogs[" + index + "].Hours")%></td>
<td>Hours</td>
</tr>
<%
index++;
}
%>
</table>
<p>
<%: Html.Hidden("InvoiceID") %>
<%: Html.Hidden("CreateDate") %>
<input type="submit" value="Save" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>
And here's the controller code:
//GET: /Secure/Invoices/LogHours/
public ActionResult LogHours(int id)
{
var invoice = DataContext.InvoiceData.Get(id);
if (invoice == null)
{
throw new Exception("Invoice not found with id: " + id);
}
return View(invoice);
}
//POST: /Secure/Invoices/SaveHours/
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveHours([Bind(Exclude = "InvoiceLogs")]Invoice invoice)
{
TryUpdateModel(invoice.InvoiceLogs, "InvoiceLogs");
invoice.UpdateDate = DateTime.Now;
invoice.DeveloperID = DeveloperID;
//attaching existing invoice.
DataContext.InvoiceData.Attach(invoice);
//save changes.
DataContext.SaveChanges();
//redirect to invoice list.
return RedirectToAction("Index");
}
And the data access code:
public static void Attach(Invoice invoice)
{
var i = new Invoice { InvoiceID = invoice.InvoiceID };
db.Invoices.Attach(i);
db.Invoices.ApplyCurrentValues(invoice);
}
In the SaveHours action, it properly sets the values of the InvoiceLog entities after I call TryUpdateModel but when it does SaveChanges it doesn't update the database with the new values. Also, if you manually update the values of the InvoiceLog entries in the database and then go to this page it doesn't populate the textboxes so it's clearly not binding correctly.
Thanks,
Justin

Got it working, I needed to populate the InvoiceLogID and InvoiceID in hidden fields so they'd get populated in the EntityCollection.

Related

how to transfer data from controller to view

HomeController.cs
[HttpPost]
public ActionResult Index(int searchtext)
{
var data = (from pm in db.ProductMasters
join shi in db.SuppliersHotelsInfoes
on pm.ProductID equals shi.LocHotelID
where shi.SearchID == searchtext
select new {pm.ProductId,pm.Image,shi.HotelName,shi.HotelPrice}).ToList().Take(10);
ViewBag.Data = data;
return View();
}
Transfering the data to the view
<% try { %>
<tr><td>
<% foreach(var item in ViewBag.Data){ %>
<img alt="" src="<%= item. %>" />
</td><%} %> <% foreach(var item in ViewBag.Data) { %>
<td><%: Html.DisplayFor(modelItem => "")%></td>
<td><%: Html.DisplayFor(modelItem => "")%> </td>
<td><%: Html.DisplayFor(modelItem => "")%></td>
</tr>
<%} }%>
<%catch(Exception ex){ }%>
Here, how to get the values in "var item"
You really should use a ViewModel, I can't remember last time I used the ViewBag
I'm also going to use Razor here, its syntax is much more terse. You don't need to use DisplayFor if you're just presenting the data:
#foreach(var item in ViewBag.Data){
<tr>
<td><img alt="" src="#item.Image" /><td>
<td>#item.ProductId</td>
<td>#item.HotelName</td>
<td>#item.HotelPrice</td>
</tr>
}

How to bind data using custom form controller to view in mvc4

mycontroller.cs
[HttpPost]
public ActionResult Index(int searchtext)
{
var data = (from pm in db.ProductMasters
join shi in db.SuppliersHotelsInfoes
on pm.ProductID equals shi.LocHotelID
where shi.SearchID == searchtext
select pm).ToList().Take(10);
ViewBag.Data = data;
return View();
}
am sending viewBag.Data to view
<% try { %>
<tr><td>
<% foreach(var item in ViewBag.Data){ %>
<img alt="" src="<%item. %>" />
</td><%} %> <% foreach(var item in ViewBag.Data) { %>
<td><%: Html.DisplayFor(modelItem => "")%></td>
<td><%: Html.DisplayFor(modelItem => "")%> </td>
<td><%: Html.DisplayFor(modelItem => "")%></td>
</tr>
<%} }%>
<%catch(Exception ex){ }%>
</table>
Here, how can we take the values from "var item"
I haven't used aspx view syntax for a while but I believe you are missing the equals sign.
You currently have this:
<img alt="" src="<%item. %>" />
instead it should be this
<img alt="" src="<%=item. %>" />
Notice the = before "item"
Take a look here for more info http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx
Also consider using a later version of MVC which supports razor syntax which is much cleaner.
Edit:
After your comment I see your issue. You are using the ViewBag which is a dynamic type. Therefore it cannot infer that your ViewBag.Data object is a collection of product masters. You have two options:
Option 1 - Don't use the ViewBag, instead use a strongly typed model.
Option 2 - In your view cast your ViewBag.Data to the List of Product Masters.
For example:
Instead of
<% foreach(var item in ViewBag.Data){ %>
Use this
<% foreach(var item in (List<ProductMaster>)ViewBag.Data){ %>

How to get confirmed list (IEnumerable) in HttpPost method?

in my ASP .Net MVC 2 application the user is shown a list of items and must click confirm before the list is persisted to the database.
When the user clicks confirm, in the HttpPost method the parameter is null. Everything works until the HttpPost method is called. At this point the list that must be persisted is null.
How do I get the confirmed values ?
I've tried using TempData in the HttpGet method but TempData is also null in the HttpPost method.
Here is the controller code.
public ActionResult Confirm()
{
List<ConfirmVehicleModel> vehicles = GetAllVehicles();
return View(vehicles);
}
[HttpPost]
public ActionResult Confirm(List<ConfirmVehicleModel> model)
{
//model is null, why ?
UploadVehiclesModelService service = new Models.UploadVehiclesModelService();
service.StoreVehicles(model, User.Identity.Name);
return RedirectToAction("Index", "UploadVehicles");
}
And here is the Confirm view:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<RM.Application.Models.ConfirmVehicleModel>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Confirm
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Confirm that the vehicles below should be added.</h2>
<table>
<tr>
<th>
ReferenceType
</th>
<th>
ReferenceName
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= Html.Encode(item.ReferenceType) %>
</td>
<td>
<%= Html.Encode(item.ReferenceName) %>
</td>
</tr>
<% } %>
</table>
<div>
<% using (Html.BeginForm())
{ %>
<input type="submit" value="Confirm" />
|
<%= Html.ActionLink("Back to upload form", "Index") %>
<% } %>
</div>
</asp:Content>
Thanks for any help,
Kind regards
Bob
Your HTML.BeginForm() is out of place it should surround the values you wish to pass.
Try:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Confirm that the vehicles below should be added.</h2>
<% using (Html.BeginForm())
{ %>
<table>
<tr>
<th>
ReferenceType
</th>
<th>
ReferenceName
</th>
</tr>
<% foreach (var item in Model)
{ %>
<tr>
<td>
<%= Html.Encode(item.ReferenceType) %>
<%= Html.HiddenFor(model => item.ReferenceType)%>
</td>
<td>
<%= Html.Encode(item.ReferenceName) %>
<%= Html.HiddenFor(model => item.ReferenceName)%>
</td>
</tr>
<% } %>
</table>
<div>
<input type="submit" value="Confirm" />
|
<%= Html.ActionLink("Back to upload form", "Index") %>
<% } %>
</div>
Nicholas advice helped. I replaced the foreach loop with a for loop and used Html.HiddenFor.
<% for (int i = 0; i < Model.Count(); i++)
{ %>
<%= Html.HiddenFor(model=>model[i].ReferenceType) %>
<%= Html.HiddenFor(model=>model[i].ReferenceName) %>
<tr>
<td>
<%= Html.Encode(Model[i].ReferenceType) %>
</td>
<td>
<%= Html.Encode(Model[i].ReferenceName) %>
</td>
</tr>
<% } %>
I also changed the top line of the the view to (previously IEnumerable, now List).
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<RM.Application.Models.ConfirmVehicleModel>>" %>\\
The List<ConfirmVehicleModel> model parameter of the HttpPost method is now populated.

ASP.NET MVC2 Application's submit button returns URL with an appended "Form"

OK, I'm sorry if the tile of the question was unclear, and if you understand what I mean, please don't hesitate to help me think of a better one.
Anyway, I have a <input type="submit"> element for my form, and I want it to return the same URL as the URL of the page the element is on.
Currently, if I click the button, it takes me from /Calculate/Quadratic to /Calculate/QuadraticForm
In my controller for this view, I have the following code:
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Quadratic()
{
ViewData["Root1"] = "";
ViewData["Root2"] = "";
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Quadratic(QuadCalc boss)
{
ViewData["Root1"] = x1;
ViewData["Root2"] = x2;
return View();
}
And here is the markup and code for my Quadratic view page, which includes the form which includes the submit button I've been referring to:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Quadratic</h2>
<% using(Html.BeginForm("QuadraticForm", "Calculate")) %>
<% { %>
<div>
a: <%= Html.TextBox("quadraticAValue") %>
<br />
b: <%= Html.TextBox("quadraticBValue") %>
<br />
c: <%= Html.TextBox("quadraticCValue") %>
<br />
<input type="submit" id="quadraticSubmitButton" value="Calculate!" />
<br />
<p><%= ViewData["Root1"] %></p>
<p><%= ViewData["Root2"] %></p>
</div>
<% } %>
</asp:Content>
Therefore, all I really want is to have the submit button return the same page, but the HTTP post will aid the application in passing new ViewData. Unless I'm interpreting this all wrong.
The problem is in your BeginForm method that calls the QuadraticForm action
<% using(Html.BeginForm("QuadraticForm", "Calculate")) %>
If you want to give an ID to the form you should use
<% using (Html.BeginForm("Quadratic", "Calculate", FormMethod.Post, new { id = "QuadraticForm" })) { %>
If you dont mind about the ID and want to just return to the same action just use
<% using(Html.BeginForm() %>
The parameters are:
The action name
The controller Name
The form method (get/post)
The form attributes

MVC Ajax not loading Partial View in div, loading new page

I'm currently working with MVC 1.0 and have never worked with AJAX before and only have limited experience (started a little more than a week ago) with ASP.Net MVC. I'm trying to setup a table (that's built / is in a partial view) that's populated with information from a db that allows a user to quickly add pr remove records into or from the db right from the table. I'm trying to use AJAX to get this done because there is a lot of other information on the rest of the page that I don't want to have to reload. Here's a quick template.
row1: (text box) Add
row2: Name1 Remove
row3: Name2 Remove
So when a user wants to, they can enter a name into the (text box) hit Add and an action in the controller will add the entered name into the database and reload the partial view with the most up to date information.
So far I have been able to get the action method to be called, and add the record into the database. The problem is that it does not reload JUST the partial-view. It instead loads the partial-view as a whole new page and it's content is the only one that displays. (Instead of staying at /Project/Details/5, the page loads /Project/MembersDisplay)
In my master page I have:
<script src="<%= Links.Scripts.jquery_1_3_2_min_js %>" type="text/javascript"></script>
<script src="<%= Links.Scripts.MicrosoftAjax_js %>" type="text/javascript"></script>
<script src="<%= Links.Scripts.MicrosoftMvcAjax_js %>" type="text/javascript"></script>
In the controller I have:
[AcceptVerbs(HttpVerbs.Post)]
public virtual ActionResult AddMember(FormCollection form)
{
MemberRepository repo = new MemberRepository();
Member m = new Member();
m.Id = Convert.ToInt32(form["Id"]);
m.Name = form["name"];
try{
repo.addMember(m);
repo.save();
}
catch{
return View();
}
// Make a new IList of All Members in this current
//IList<Member> mems = (new PRDataContext()).Members_GetMembersById(m.Id).ToList<Member>();
return View("MembersDisplay", members);
}
The Partial view is
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<Application.Models.Member>>" %>
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoffMvcAjax.js" type="text/javascript"></script>
<table>
<% if (Model.Count() > 0) { %>
<% foreach (var p in ViewData.Model) { %>
<tr>
<td><%= Html.Encode(p.Name) %></td>
<td><%= Ajax.ActionLink("Delete", "AjaxDelete", new { p.Id, p.Name }, new AjaxOptions { Confirm = "Delete?", HttpMethod = "AjaxDelete", UpdateTargetId = "results" })%></td>
</tr>
<% } %>
<% } else { %>
<tr>
<td><b>There are no Members.</b></td>
</tr>
<% } %>
</table>
In the controller, I tried adding if(Request.IsAjaxRequest()==true) the branch never executed. I'm not sure if ajax isn't loading, or what I'm doing wrong.
EDIT - Added missing code
Here's the details portion that uses the partial view:
<table>
<% Ajax.BeginForm("AddMember", "Project", new AjaxOptions { UpdateTargetId = "results", InsertionMode=InsertionMode.Replace}) { %>
<tr>
<td align="left" colspan="2" style="background-color:#93B6E0;"><font style="color:#fff;"><b>Members</b></font></td>
</tr>
<tr>
<td width="80%">
<input type="hidden" name="prjId" value="<%= Html.Encode(Model.Id) %>" /><input type="text" name="name" style="width:120px;" />
</td>
<td width="20%">
<input type="submit" value="Add" />
</td>
</tr>
<% } %>
<tr>
<td colspan="2">
<div id="results"><% Html.RenderPartial(MVC.Members.Views.MembersDisplay, Model.Members.ToList<Member>()); %></div>
</td>
</tr>
Dirk - not sure if the complete code is there. however, from a brief glance, your returning:
return View("MembersDisplay", members);
when in fact, for a partial view, you should be returning something like:
return PartialView("MembersDisplay", members);
hope this helps
jim
If you're intending the submit to fire the Ajax.BeginForm, you'll need to include it in the form with curly braces. Currently your submit is not within the form.
I upgraded to MVC 2.0 as #RailRhoad suggested.
Then I moved the partial view to an area and has to change the path links to the .js files to be more explicit (T4MVC wasn't rendering the correct paths). Instead of
<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
or
<script src="<%= Links.Scripts.MicrosoftAjax_js %>" type="text/javascript"></script>
I had to do
<script src="../../../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
Also, the action in the controller worked as #jim suggested by
return PartialView(MVC.ProjectDash.Project.Views.MembersDisplay, members);
Also, I removed the "InsertionMode=..." from
<% Ajax.BeginForm("AddMember", "Project", new AjaxOptions { UpdateTargetId = "results", InsertionMode=InsertionMode.Replace}) { %>
The biggest issue was the javascript files were not be linked up and referenced correctly. Once that was fixed, the other things quickly feel into place.

Resources