1

I am converting a project from ASP.net Forms to ASP.Net MVC Core.

Consider the following form: enter image description here

I'm creating a new recipient, and that recipient can have multiple contact methods.

The Add Contact Methods button brings up the following Modal: enter image description here

In my ASP.Net forms app, I had a repeater, and I just added a new element to the repeater, and then when the form was submitted, I would iterate through the repeater's rows and populate my database.

How would one do this in MVC? Do I just create an html table and iterate through those rows?

I am new to MVC, so I am not sure how to proceed here.

For reference here is my data model for recipients and their contact methods:

public class Recipient
{
    [Key]
    public Guid RecipientGUID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Company { get; set; }

    public UserGroup Owner { get; set; }

    public List<ContactMethod> ContactMethods { get; set; }

    public User CreatedBy { get; set; }

    public DateTime CreatedOn { get; set; }

    public User LastModifiedBy { get; set; }

    public DateTime LastModifiedOn { get; set; }

    public bool IsActive { get; set; }

}

Contact Methods:

public class ContactMethod
{
    [Key]
    [HiddenInput(DisplayValue = false)]
    public Guid ContactMethodGUID { get; set; }

    [Required(ErrorMessage = "Please specify a type.")]
    public ContactMethodType Type { get; set; }

    [Required]
    public Recipient Recipient { get; set; }

    public int CountryCode { get; set; }

    [Required(ErrorMessage = "Please enter a identifier.")]
    public string Identifier { get; set; }

    public bool IsPreferred { get; set; }
}
ArtOfCode
  • 5,702
  • 5
  • 37
  • 56
MarekT
  • 104
  • 8
  • Refer [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for options to dynamically add collection items –  Nov 03 '16 at 20:24

1 Answers1

2

You use Razor syntax in your .cshtml view file.

<table>
@foreach var recipient in model.Recipients 
{
    <tr><td>@recipient.FirstName</td></tr>
}
</table>

Look up a ASP.NET MVC Razor tutorial

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
  • @recipient.FirstName – James Nov 03 '16 at 18:09
  • 1
    @Ya I was thinking, because I am in an @{ //code} block that I do not need the @... – Brian Ogden Nov 03 '16 at 18:12
  • Thanks for that, but how would I add a new row to the collection? Can razor do that? or do I have to call the controller, and post the data back? So I have a recipient, and I want to add a telephone and email to their contact methods list. that is the part that is getting me. – MarekT Nov 03 '16 at 20:25
  • Yes you need to post data back to server to add a telephone number – Brian Ogden Nov 03 '16 at 20:27