0

i have two entity: 1) student and 2) address.

public class Student 
{
    Public Int StudentId { get; set; }
    Public String FullName { get; set; }

    Public virtual IList<Address> Addresses { get; set; }
}

public class Address
{
    Public Int AddressId { get; set; }
    Public Int StudentId { get; set; }
    Public String FullAddress { get; set; }

    Public virtual Student Student { get; set; }
}

each student may have zero or more address.

i want to create single view for this two entity. i know that must create a view model. this is view model.

public class StudentViewModel
{
    Public Int StudentId { get; set; }
    Public String FullName { get; set; }
    public Address AddAddressModel { get; set; }

    Public virtual IList<Address> Addresses { get; set; }
}

and i create a view for StudentViewModel. this is StudentViewModel:

@model MyProject.Models.StudentViewModel

@{
    ViewBag.Title = "Create";
 }

 @using (Html.BeginForm ())
 {
     @Html.ValidationSummary(true)

    <fieldset>

        <div class="editor-label">
            @Html.LabelFor(model => model.FullName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FullName)
            @Html.ValidationMessageFor(model => model.FullName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AddAddressModel.FullAddress)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AddAddressModel.FullAddres)
            @Html.ValidationMessageFor(model => model.AddAddressModel.FullAddres)
        </div>

        <button id="add">add address to list</button>

        <input type="submit" value="save in database" />        
</fieldset>

please tell me how i can add one or more address on by one in Addresses Property of StudentViewModel and show after this operation to user. finally when i click on the "save in database" button student and his addresses must be inserted in database.

tereško
  • 58,060
  • 25
  • 98
  • 150
Pezhman Parsaee
  • 1,209
  • 3
  • 21
  • 35

2 Answers2

1

I have in the past submitted nested child entities, but that was always done with an API call and the form was serialized to a JSON object before submission.

Because you only really need multiple FullAddress values, you could change your model and view accordingly (not tested):

Model:

public class StudentViewModel
{
    public int StudentId { get; set; }
    public string FullName { get; set; }
    public string[] Addresses { get; set; }
}

View:

In your view, when you click the 'Add' button, make sure that (through JavaScript) you end up with something like this:

<textarea name="Addresses[]">Some Address 1</textarea>
<textarea name="Addresses[]">Some Address 2</textarea>
etc...
Knelis
  • 6,782
  • 2
  • 34
  • 54
  • thank you CornéM, but i want to use my model. bacause i want to extend this model to complex model like this: factory and his branches. please tell me more about first solution: serializing form to JSON Object before form submission – Pezhman Parsaee Feb 22 '13 at 13:15
  • 1
    Hi, you can serialize forms to complex JSON objects using this plugin: http://stackoverflow.com/a/8407771/1746803. You can then simply use `$.post()` to send the object to your script. I have used this solution many times with complex models and it has yet to fail me. – Knelis Feb 22 '13 at 15:04
0

you can try using:

@EditorFor(m => model.Addresses)

this will create template for your address model

Rusty
  • 1,303
  • 2
  • 14
  • 28
  • thank you, but bigger problem is how to save addresses bofore click on "save in database" button and insert this data in database after click. – Pezhman Parsaee Feb 22 '13 at 13:19
  • yes when u post the form, you should get values under yourviewmodel.Addresses.AddressId or yourviewmodel.Addresses.FullAddress – Rusty Feb 22 '13 at 13:26
  • how i add address to list before form submission? jQuery has any help for me? after sumbit of form, in Create Action Method i want to access student addresses by writing model.Addresses – Pezhman Parsaee Feb 22 '13 at 13:34