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; }
}
1
  • Refer this answer for options to dynamically add collection items Commented Nov 3, 2016 at 20:24

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

@recipient.FirstName
@Ya I was thinking, because I am in an @{ //code} block that I do not need the @...
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.
Yes you need to post data back to server to add a telephone number

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.