简体   繁体   中英

Storing and posting multiple rows of data ASP.Net MVC

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

Consider the following form: 在此处输入图片说明

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

The Add Contact Methods button brings up the following Modal: 在此处输入图片说明

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; }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM