views:

53

answers:

2

I'm a beginner in ASP.NET MVC application. What I'm trying to do is creating a form with some inputs that the user will be filling up, and once the user click the post button, I want the form to be posted with the information filled up and ready for printing. The way I'm doing it right now is as follow:

// the controller that returns the initial form using ReportCreate.aspx which creates a Html form
public ActionResult ReportCreate()
{
    return View(viewData);
}


// my post action which gets the information for the submitted form
// and use the ReportPost.aspx to view a similar page as ReportCreate.aspx but with all the Html.TexBox inputs replaced with their values obtained from the submitted form      
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ReportCreate(FormCollection form)
{
    ReportFormData formData = new ReportFormData();

    formData.Date = form["date"];
    formData.Company = form["company"];
    formData.SiteNameA = form["siteNameA"];
    formData.SiteNameB = form["siteNameB"];
    formData.FreqBand = form["freqBand"];
    formData.FileNumber = form["fileNumber"];
    formData.ResponseDate = form["responseDate"];

    formData.SiteAddressA = form["siteAddressA"];
    formData.SiteAddressB = form["siteAddressB"];

    this.TempData.Add("viewData", viewData);

    return View("ReportPost", formData);
}

What I don't like about this way, is that I have to aspx pages (ReportCreate.aspx & ReportPost.aspx) that I need to keep similar and modify both of them together if I need to do any changes to the look of the form. I feel there should be a more professional way to handle this common issue. I tried to look it up online, but couldn't get anything. Please let me know. Thanks a lot in advance.

A: 

If all the formatting is the same other than the textbox should be a label, just use a conditional in your view to determine if you should display a textbox or not.

<%if(model.ReadOnly){%><%=Html.LabelFor(m => m.Company)%><%else%><%=Html.TextBoxFor(m => m.Company)%><%}%>
Brian
+2  A: 

If you want to display the posted data in the same form just use the same aspx page as when you created the data.

However the usual way is to have one page for:

  • Create - to input values first time and after a succesful input redirect to
  • Details - where the data is not on a form but as regular text

If you need to modify the data use

  • Edit

To display a collection of data use

  • Index

Another point to note is that you dont need to use manually set all of the values from the form to your ReportFormData class, instead do:

[HttpPost]
public ActionResult Create(ReportFormData formData) 
{
  if(!ModelState.Isvalid){
     return View(formData);
  }
  else
  {
   RedirectToAction("Index");
  } 
}
Henrik
+1 for suggesting the usage of a view model.
Darin Dimitrov
thanks, I don't see how the inputs from my form will get bind to the instance variables inside my ReportFormData object. Do I need to do anything special, or it will happen automatically. I tried it and it doesn't seem to work.
Abdul Salama
Make sure you have the name attribute set correctly on all input elements. <input name="company" value="Microsoft"> will bind "Microsoft" to the company property on your formdata instance.
Henrik