0

I have an USER and a CAR class.

I know how build an USER CREATE form, naming simple element same as the field on db (like name, phonenumber) so on SUBMIT the Action method get every element and I can save it to db.

Now on the CREATE form I also want user add CARS, and on SUBMIT send List.

How I do that?

For example I can return a collection like this Or return one car element, creating an input for each car property.

<form method="post" action="/Home/UpdateInts">
     <input type="text" name="cars" value="1" />
     <input type="text" name="cars" value="4" />
     <input type="text" name="cars" value="2" />
     <input type="text" name="cars" value="8" />
     <input type="submit" />
</form>

public ActionResult UpdateInts(ICollection<int> cars) {
  SaveMethod(cars);
}

But how I do to receive

public ActionResult UpdateInts(ICollection<Car> myCars) {
  SaveMethod(myCars);
}

Cuurently Im using jQuery EasyUI grid To add cars, make and model during the create user page.

3
  • So you want to save a user and a car on one submit? Commented Mar 27, 2015 at 17:22
  • one user and N cars in one submit Commented Mar 27, 2015 at 17:25
  • You need to generate controls with the correct name attributes with indexers - name="[0].Make", name="[0].Model", name="[1].Make", name="[1].Model" etc. Some options to consider for dynamically creating new items in a view here and here Commented Mar 28, 2015 at 1:21

2 Answers 2

1

I solved it by using raw form data. So there was no need of passing a model to the view (No a MVC pattern). This is not the best solution but a working solution. I will update soon if I come up with better solution.

Controller:

[HttpPost]
public ActionResult AddCars(int a = 0){
        var context = new ApplicationDbContext();
        var form = Request.Form;
        for (int i = 1; i < form.Count; i ++ )
        {
            context.Cars.Add(new Car() { Name = form[i] });
        }
        context.SaveChanges();
        return View();
}

View:

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>Car</h4>
        <hr />
        <input type="text" name="car1" value="car1" />
        <input type="text" name="car2" value="car2" />
        <input type="text" name="car3" value="car3" />
        <input type="text" name="car4" value="car4" />
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Model

public class Car{
    public int Id { get; set; }
    public string Name { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Make a view model of your user with collection of cars inside.

public clas ViewModel
{
    //User properties
    public IEnumerable<Car> Cars {get; set;}
}

The use this View Model in your View for Create/Edit:

@model ViewModel
@using (Html.BeginForm()) 
{
    //User Editor Here

    @for(int i=0; i <= Model.Cars.Count(); i++)
    {
        @Html.EditorFor(x => Model.Cars[i])
    }
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

And the action

[HttpPost]
public ActionResult UpdateInts(ViewModel viewModel) {
  //Get your user and cars here and save them
}

2 Comments

That look good for Update. But I think I can make it work for Create with Model.Car.add(newCar) before post.
When you submit, you can create your user and fill its collection of cars. Then save/update to your db.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.