Building form in MVC

Hi,

I ma trying to build a form in a MVC view. I have been looking at this: http://blog.michaelckennedy.net/2012/01/20/building-asp-net-mvc-forms-with-razor/ which seems to be appropriate. My issue is that I am passing a composite data model because the layout page needs its data.

My model looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RTSOL.Models
{
    public class Multi
    {
        public IEnumerable<SubLinks> submenu { get; set; }
        public IEnumerable<Product> products { get; set; }
        public IEnumerable<Category> categories { get; set; }
        public IEnumerable<QuoteForm> formdata { get; set; }
    }
}

Open in new window

The model for the form is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RTSOL.Models
{
    public class QuoteForm
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

Open in new window

My view looks like this:
@{
    ViewBag.Title = "Offertformulär";
}

@model RTSOL.Models.Multi

<h2>Offertformulär</h2>
@{
    using (Html.BeginForm())
    {
        <div>
            @Html.TextBoxFor(model => model.formdata.FirstName)
        </div>
        <div>
            @Html.TextBoxFor(model => model.formdata.LastName)
        </div>
        <button type="submit" title="Skicka!" class="contactbutton" name="contactb" value="contactSite">Skicka offerten!</button>
    }

}

Open in new window

and here is my controller:
        [HttpGet]
        public ActionResult Offertformular()
        {
            getViewBag();

            ViewBag.leftClass = "leftColumnFull";

            List<SubLinks> sublinks = new List<SubLinks>();
            List<Product> prods = new List<Product>();
            List<QuoteForm> formdata = new List<QuoteForm>();
            QuoteForm q1 = new QuoteForm();
            q1.FirstName = "Robert";
            q1.LastName = "Tell";
            formdata.Add(q1);
            Multi data = new Multi();
            data.submenu = sublinks;
            data.products = prods;
            data.formdata = formdata;

            return View(data);
        }

        [HttpPost]
        public ActionResult Offertformular(Multi dataret)
        {
            List<SubLinks> sublinks = new List<SubLinks>();
            List<Product> prods = new List<Product>();
            Multi data = new Multi();
            data.submenu = sublinks;
            data.products = prods;

            return View("offertTack", data);
        }

Open in new window


It seems I am missing something. In the line @Html.TextBoxFor(model => model.formdata.FirstName) Firstname is not recognized.

I would appreciate your help to put things right.

Best regards
RTSOL
RTSolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

Your problem is that formdata in Multi model is declared as IEnumerable. That means that formdata is not a single QuoteForm but rather a "collection" of Quoteforms.

I guess you would need only one form on the page. If indeed Multi should have one form then you should declare formdata as:

public QuoteForm formdata { get; set; }

Open in new window


If you need multiple forms, then you i think you should do some more extended modifications. But before i get into details, i would like to know if i am correct with my assumption that you need only one form or not.

Giannis
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RTSolAuthor Commented:
Of course - how stupid I am. Thanks a lot. I have two forms on the site but my multi now looks like this:

    public class Multi
    {
        public IEnumerable<SubLinks> submenu { get; set; }
        public IEnumerable<Product> products { get; set; }
        public IEnumerable<Category> categories { get; set; }
        public QuoteForm formd { get; set; }
        public EmpForm empd { get; set; }
    }

and all is fine.

Best regads
RTSOL
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Applications

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.