how i can send multivalue to create action












0















i have a doctor i want add doctor subspecialty to the doctor from sub specialties table many to many relationship



i need to add subspecialties from multiselect list but my controller only add first selection , i want my create controller take all passed subspecialties and create it



my model



public partial class DoctorSubSpecialty
{
public int Id { get; set; }
public Nullable<int> DoctorId { get; set; }
public Nullable<int> SubSpecialtyId { get; set; }

public virtual DoctorProfile DoctorProfile { get; set; }
public virtual SubSpecialty SubSpecialty { get; set; }
}


}



create get action



public ActionResult Create()
{
ViewBag.DoctorId = new SelectList(db.DoctorProfiles, "Id", "FullName");
ViewBag.SubSpecialtyId = new MultiSelectList(db.SubSpecialties, "id", "Name");
return View();
}


create post action



 [HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "Id,DoctorId,SubSpecialtyId")] DoctorSubSpecialty doctorSubSpecialty)
{

DoctorSubSpecialty doctorSub = db.DoctorSubSpecialties.Where(d => d.DoctorId == doctorSubSpecialty.DoctorId & d.SubSpecialtyId == doctorSubSpecialty.SubSpecialtyId).FirstOrDefault();
if (doctorSub == null) {
db.DoctorSubSpecialties.Add(doctorSubSpecialty);
await db.SaveChangesAsync();
}


my view



@using (Html.BeginForm()) 


{
@Html.AntiForgeryToken()



<div class="form-horizontal">
<h4>DoctorSubSpecialty</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.DoctorId, "DoctorId", htmlAttributes: new { @class = "control-label col-md-2", @id = "DoctorID" })
<div class="col-md-10">
@Html.DropDownList("DoctorId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.DoctorId, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.SubSpecialtyId, "SubSpecialtyId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("SubSpecialtyId",(MultiSelectList)ViewBag.SubSpecialtyId, htmlAttributes: new { @multiple = "multiple", @class = "form-control" })
@Html.ValidationMessageFor(model => model.SubSpecialtyId, "", new { @class = "text-danger" })
</div>
</div>

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


}










share|improve this question



























    0















    i have a doctor i want add doctor subspecialty to the doctor from sub specialties table many to many relationship



    i need to add subspecialties from multiselect list but my controller only add first selection , i want my create controller take all passed subspecialties and create it



    my model



    public partial class DoctorSubSpecialty
    {
    public int Id { get; set; }
    public Nullable<int> DoctorId { get; set; }
    public Nullable<int> SubSpecialtyId { get; set; }

    public virtual DoctorProfile DoctorProfile { get; set; }
    public virtual SubSpecialty SubSpecialty { get; set; }
    }


    }



    create get action



    public ActionResult Create()
    {
    ViewBag.DoctorId = new SelectList(db.DoctorProfiles, "Id", "FullName");
    ViewBag.SubSpecialtyId = new MultiSelectList(db.SubSpecialties, "id", "Name");
    return View();
    }


    create post action



     [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Create([Bind(Include = "Id,DoctorId,SubSpecialtyId")] DoctorSubSpecialty doctorSubSpecialty)
    {

    DoctorSubSpecialty doctorSub = db.DoctorSubSpecialties.Where(d => d.DoctorId == doctorSubSpecialty.DoctorId & d.SubSpecialtyId == doctorSubSpecialty.SubSpecialtyId).FirstOrDefault();
    if (doctorSub == null) {
    db.DoctorSubSpecialties.Add(doctorSubSpecialty);
    await db.SaveChangesAsync();
    }


    my view



    @using (Html.BeginForm()) 


    {
    @Html.AntiForgeryToken()



    <div class="form-horizontal">
    <h4>DoctorSubSpecialty</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
    @Html.LabelFor(model => model.DoctorId, "DoctorId", htmlAttributes: new { @class = "control-label col-md-2", @id = "DoctorID" })
    <div class="col-md-10">
    @Html.DropDownList("DoctorId", null, htmlAttributes: new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.DoctorId, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.SubSpecialtyId, "SubSpecialtyId", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.DropDownList("SubSpecialtyId",(MultiSelectList)ViewBag.SubSpecialtyId, htmlAttributes: new { @multiple = "multiple", @class = "form-control" })
    @Html.ValidationMessageFor(model => model.SubSpecialtyId, "", new { @class = "text-danger" })
    </div>
    </div>

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


    }










    share|improve this question

























      0












      0








      0








      i have a doctor i want add doctor subspecialty to the doctor from sub specialties table many to many relationship



      i need to add subspecialties from multiselect list but my controller only add first selection , i want my create controller take all passed subspecialties and create it



      my model



      public partial class DoctorSubSpecialty
      {
      public int Id { get; set; }
      public Nullable<int> DoctorId { get; set; }
      public Nullable<int> SubSpecialtyId { get; set; }

      public virtual DoctorProfile DoctorProfile { get; set; }
      public virtual SubSpecialty SubSpecialty { get; set; }
      }


      }



      create get action



      public ActionResult Create()
      {
      ViewBag.DoctorId = new SelectList(db.DoctorProfiles, "Id", "FullName");
      ViewBag.SubSpecialtyId = new MultiSelectList(db.SubSpecialties, "id", "Name");
      return View();
      }


      create post action



       [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task<ActionResult> Create([Bind(Include = "Id,DoctorId,SubSpecialtyId")] DoctorSubSpecialty doctorSubSpecialty)
      {

      DoctorSubSpecialty doctorSub = db.DoctorSubSpecialties.Where(d => d.DoctorId == doctorSubSpecialty.DoctorId & d.SubSpecialtyId == doctorSubSpecialty.SubSpecialtyId).FirstOrDefault();
      if (doctorSub == null) {
      db.DoctorSubSpecialties.Add(doctorSubSpecialty);
      await db.SaveChangesAsync();
      }


      my view



      @using (Html.BeginForm()) 


      {
      @Html.AntiForgeryToken()



      <div class="form-horizontal">
      <h4>DoctorSubSpecialty</h4>
      <hr />
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
      <div class="form-group">
      @Html.LabelFor(model => model.DoctorId, "DoctorId", htmlAttributes: new { @class = "control-label col-md-2", @id = "DoctorID" })
      <div class="col-md-10">
      @Html.DropDownList("DoctorId", null, htmlAttributes: new { @class = "form-control" })
      @Html.ValidationMessageFor(model => model.DoctorId, "", new { @class = "text-danger" })
      </div>
      </div>

      <div class="form-group">
      @Html.LabelFor(model => model.SubSpecialtyId, "SubSpecialtyId", htmlAttributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
      @Html.DropDownList("SubSpecialtyId",(MultiSelectList)ViewBag.SubSpecialtyId, htmlAttributes: new { @multiple = "multiple", @class = "form-control" })
      @Html.ValidationMessageFor(model => model.SubSpecialtyId, "", new { @class = "text-danger" })
      </div>
      </div>

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


      }










      share|improve this question














      i have a doctor i want add doctor subspecialty to the doctor from sub specialties table many to many relationship



      i need to add subspecialties from multiselect list but my controller only add first selection , i want my create controller take all passed subspecialties and create it



      my model



      public partial class DoctorSubSpecialty
      {
      public int Id { get; set; }
      public Nullable<int> DoctorId { get; set; }
      public Nullable<int> SubSpecialtyId { get; set; }

      public virtual DoctorProfile DoctorProfile { get; set; }
      public virtual SubSpecialty SubSpecialty { get; set; }
      }


      }



      create get action



      public ActionResult Create()
      {
      ViewBag.DoctorId = new SelectList(db.DoctorProfiles, "Id", "FullName");
      ViewBag.SubSpecialtyId = new MultiSelectList(db.SubSpecialties, "id", "Name");
      return View();
      }


      create post action



       [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task<ActionResult> Create([Bind(Include = "Id,DoctorId,SubSpecialtyId")] DoctorSubSpecialty doctorSubSpecialty)
      {

      DoctorSubSpecialty doctorSub = db.DoctorSubSpecialties.Where(d => d.DoctorId == doctorSubSpecialty.DoctorId & d.SubSpecialtyId == doctorSubSpecialty.SubSpecialtyId).FirstOrDefault();
      if (doctorSub == null) {
      db.DoctorSubSpecialties.Add(doctorSubSpecialty);
      await db.SaveChangesAsync();
      }


      my view



      @using (Html.BeginForm()) 


      {
      @Html.AntiForgeryToken()



      <div class="form-horizontal">
      <h4>DoctorSubSpecialty</h4>
      <hr />
      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
      <div class="form-group">
      @Html.LabelFor(model => model.DoctorId, "DoctorId", htmlAttributes: new { @class = "control-label col-md-2", @id = "DoctorID" })
      <div class="col-md-10">
      @Html.DropDownList("DoctorId", null, htmlAttributes: new { @class = "form-control" })
      @Html.ValidationMessageFor(model => model.DoctorId, "", new { @class = "text-danger" })
      </div>
      </div>

      <div class="form-group">
      @Html.LabelFor(model => model.SubSpecialtyId, "SubSpecialtyId", htmlAttributes: new { @class = "control-label col-md-2" })
      <div class="col-md-10">
      @Html.DropDownList("SubSpecialtyId",(MultiSelectList)ViewBag.SubSpecialtyId, htmlAttributes: new { @multiple = "multiple", @class = "form-control" })
      @Html.ValidationMessageFor(model => model.SubSpecialtyId, "", new { @class = "text-danger" })
      </div>
      </div>

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


      }







      asp.net-mvc model-view-controller entity-framework-6






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 12:29









      Mahmoud Abdel-hamidMahmoud Abdel-hamid

      112




      112
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Create a ViewModel specific to your usecase that can actually transport more than one Id.
          I.e. you will need an int to bind the selection to.
          A ViewModel also helps you to get rid of all this ViewBag and [Bind] nonsense.



          public class CreateDoctorSubSpecialtyViewModel {

          // These are the selected values to be posted back
          public int DoctorId { get; set; }
          public int SubSpecialtyIds { get; set; }

          // These are the possible values for the dropdowns
          public IEnumerable<SelectListItem> DoctorProfiles { get; set; }
          public IEnumerable<SelectListItem> SubSpecialties { get; set; }
          }


          GET action - initialize the ViewModel and pass it to the View:



          [HttpGet]
          public ActionResult Create() {

          var doctorProfiles = db.DoctorProfiles.Select(d =>
          new SelectListItem {
          Text = d.FullName,
          Value = d.Id
          }
          ).ToArray();

          var subSpecialties = db.SubSpecialties.Select(s =>
          new SelectListItem {
          Text = s.Name,
          Value = s.id
          }
          ).ToArray();

          var viewModel = new CreateDoctorSubSpecialtyViewModel {
          DoctorProfiles = doctorProfiles,
          SubSpecialties = subSpecialties
          };

          return View("Create", viewModel);
          }


          View "Create.cshtml" (styling removed for clarity) - tell MVC which ViewModel we want to use with @model:



          @model CreateDoctorSubSpecialtyViewModel
          @using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post)) {

          @Html.DropDownListFor(m => m.DoctorId, Model.DoctorProfiles)

          @Html.DropDownListFor(m => m.SubSpecialtyIds, Model.SubSpecialties, new { multiple = "multiple" })

          <input type="submit" />
          }


          POST action - use Linq Contains to test against multiple submitted SubSpecialtyIds:



          [HttpPost]
          [ValidateAntiForgeryToken]
          public async Task<ActionResult> Create(CreateDoctorSubSpecialtyViewModel postData) {

          DoctorSubSpecialty allSelectedSubSpecialities = db.DoctorSubSpecialties
          .Where(d => d.DoctorId == postData.DoctorId
          && postData.SubSpecialtyIds.Contains(d.SubSpecialtyId))
          .ToArray();

          // ...
          }




          EDIT @Html.DropDownListFor requires an IEnumerable<SelectListItem> as second parameter.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499734%2fhow-i-can-send-multivalue-to-create-action%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Create a ViewModel specific to your usecase that can actually transport more than one Id.
            I.e. you will need an int to bind the selection to.
            A ViewModel also helps you to get rid of all this ViewBag and [Bind] nonsense.



            public class CreateDoctorSubSpecialtyViewModel {

            // These are the selected values to be posted back
            public int DoctorId { get; set; }
            public int SubSpecialtyIds { get; set; }

            // These are the possible values for the dropdowns
            public IEnumerable<SelectListItem> DoctorProfiles { get; set; }
            public IEnumerable<SelectListItem> SubSpecialties { get; set; }
            }


            GET action - initialize the ViewModel and pass it to the View:



            [HttpGet]
            public ActionResult Create() {

            var doctorProfiles = db.DoctorProfiles.Select(d =>
            new SelectListItem {
            Text = d.FullName,
            Value = d.Id
            }
            ).ToArray();

            var subSpecialties = db.SubSpecialties.Select(s =>
            new SelectListItem {
            Text = s.Name,
            Value = s.id
            }
            ).ToArray();

            var viewModel = new CreateDoctorSubSpecialtyViewModel {
            DoctorProfiles = doctorProfiles,
            SubSpecialties = subSpecialties
            };

            return View("Create", viewModel);
            }


            View "Create.cshtml" (styling removed for clarity) - tell MVC which ViewModel we want to use with @model:



            @model CreateDoctorSubSpecialtyViewModel
            @using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post)) {

            @Html.DropDownListFor(m => m.DoctorId, Model.DoctorProfiles)

            @Html.DropDownListFor(m => m.SubSpecialtyIds, Model.SubSpecialties, new { multiple = "multiple" })

            <input type="submit" />
            }


            POST action - use Linq Contains to test against multiple submitted SubSpecialtyIds:



            [HttpPost]
            [ValidateAntiForgeryToken]
            public async Task<ActionResult> Create(CreateDoctorSubSpecialtyViewModel postData) {

            DoctorSubSpecialty allSelectedSubSpecialities = db.DoctorSubSpecialties
            .Where(d => d.DoctorId == postData.DoctorId
            && postData.SubSpecialtyIds.Contains(d.SubSpecialtyId))
            .ToArray();

            // ...
            }




            EDIT @Html.DropDownListFor requires an IEnumerable<SelectListItem> as second parameter.






            share|improve this answer






























              1














              Create a ViewModel specific to your usecase that can actually transport more than one Id.
              I.e. you will need an int to bind the selection to.
              A ViewModel also helps you to get rid of all this ViewBag and [Bind] nonsense.



              public class CreateDoctorSubSpecialtyViewModel {

              // These are the selected values to be posted back
              public int DoctorId { get; set; }
              public int SubSpecialtyIds { get; set; }

              // These are the possible values for the dropdowns
              public IEnumerable<SelectListItem> DoctorProfiles { get; set; }
              public IEnumerable<SelectListItem> SubSpecialties { get; set; }
              }


              GET action - initialize the ViewModel and pass it to the View:



              [HttpGet]
              public ActionResult Create() {

              var doctorProfiles = db.DoctorProfiles.Select(d =>
              new SelectListItem {
              Text = d.FullName,
              Value = d.Id
              }
              ).ToArray();

              var subSpecialties = db.SubSpecialties.Select(s =>
              new SelectListItem {
              Text = s.Name,
              Value = s.id
              }
              ).ToArray();

              var viewModel = new CreateDoctorSubSpecialtyViewModel {
              DoctorProfiles = doctorProfiles,
              SubSpecialties = subSpecialties
              };

              return View("Create", viewModel);
              }


              View "Create.cshtml" (styling removed for clarity) - tell MVC which ViewModel we want to use with @model:



              @model CreateDoctorSubSpecialtyViewModel
              @using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post)) {

              @Html.DropDownListFor(m => m.DoctorId, Model.DoctorProfiles)

              @Html.DropDownListFor(m => m.SubSpecialtyIds, Model.SubSpecialties, new { multiple = "multiple" })

              <input type="submit" />
              }


              POST action - use Linq Contains to test against multiple submitted SubSpecialtyIds:



              [HttpPost]
              [ValidateAntiForgeryToken]
              public async Task<ActionResult> Create(CreateDoctorSubSpecialtyViewModel postData) {

              DoctorSubSpecialty allSelectedSubSpecialities = db.DoctorSubSpecialties
              .Where(d => d.DoctorId == postData.DoctorId
              && postData.SubSpecialtyIds.Contains(d.SubSpecialtyId))
              .ToArray();

              // ...
              }




              EDIT @Html.DropDownListFor requires an IEnumerable<SelectListItem> as second parameter.






              share|improve this answer




























                1












                1








                1







                Create a ViewModel specific to your usecase that can actually transport more than one Id.
                I.e. you will need an int to bind the selection to.
                A ViewModel also helps you to get rid of all this ViewBag and [Bind] nonsense.



                public class CreateDoctorSubSpecialtyViewModel {

                // These are the selected values to be posted back
                public int DoctorId { get; set; }
                public int SubSpecialtyIds { get; set; }

                // These are the possible values for the dropdowns
                public IEnumerable<SelectListItem> DoctorProfiles { get; set; }
                public IEnumerable<SelectListItem> SubSpecialties { get; set; }
                }


                GET action - initialize the ViewModel and pass it to the View:



                [HttpGet]
                public ActionResult Create() {

                var doctorProfiles = db.DoctorProfiles.Select(d =>
                new SelectListItem {
                Text = d.FullName,
                Value = d.Id
                }
                ).ToArray();

                var subSpecialties = db.SubSpecialties.Select(s =>
                new SelectListItem {
                Text = s.Name,
                Value = s.id
                }
                ).ToArray();

                var viewModel = new CreateDoctorSubSpecialtyViewModel {
                DoctorProfiles = doctorProfiles,
                SubSpecialties = subSpecialties
                };

                return View("Create", viewModel);
                }


                View "Create.cshtml" (styling removed for clarity) - tell MVC which ViewModel we want to use with @model:



                @model CreateDoctorSubSpecialtyViewModel
                @using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post)) {

                @Html.DropDownListFor(m => m.DoctorId, Model.DoctorProfiles)

                @Html.DropDownListFor(m => m.SubSpecialtyIds, Model.SubSpecialties, new { multiple = "multiple" })

                <input type="submit" />
                }


                POST action - use Linq Contains to test against multiple submitted SubSpecialtyIds:



                [HttpPost]
                [ValidateAntiForgeryToken]
                public async Task<ActionResult> Create(CreateDoctorSubSpecialtyViewModel postData) {

                DoctorSubSpecialty allSelectedSubSpecialities = db.DoctorSubSpecialties
                .Where(d => d.DoctorId == postData.DoctorId
                && postData.SubSpecialtyIds.Contains(d.SubSpecialtyId))
                .ToArray();

                // ...
                }




                EDIT @Html.DropDownListFor requires an IEnumerable<SelectListItem> as second parameter.






                share|improve this answer















                Create a ViewModel specific to your usecase that can actually transport more than one Id.
                I.e. you will need an int to bind the selection to.
                A ViewModel also helps you to get rid of all this ViewBag and [Bind] nonsense.



                public class CreateDoctorSubSpecialtyViewModel {

                // These are the selected values to be posted back
                public int DoctorId { get; set; }
                public int SubSpecialtyIds { get; set; }

                // These are the possible values for the dropdowns
                public IEnumerable<SelectListItem> DoctorProfiles { get; set; }
                public IEnumerable<SelectListItem> SubSpecialties { get; set; }
                }


                GET action - initialize the ViewModel and pass it to the View:



                [HttpGet]
                public ActionResult Create() {

                var doctorProfiles = db.DoctorProfiles.Select(d =>
                new SelectListItem {
                Text = d.FullName,
                Value = d.Id
                }
                ).ToArray();

                var subSpecialties = db.SubSpecialties.Select(s =>
                new SelectListItem {
                Text = s.Name,
                Value = s.id
                }
                ).ToArray();

                var viewModel = new CreateDoctorSubSpecialtyViewModel {
                DoctorProfiles = doctorProfiles,
                SubSpecialties = subSpecialties
                };

                return View("Create", viewModel);
                }


                View "Create.cshtml" (styling removed for clarity) - tell MVC which ViewModel we want to use with @model:



                @model CreateDoctorSubSpecialtyViewModel
                @using (Html.BeginForm("Create", "YourControllerName", FormMethod.Post)) {

                @Html.DropDownListFor(m => m.DoctorId, Model.DoctorProfiles)

                @Html.DropDownListFor(m => m.SubSpecialtyIds, Model.SubSpecialties, new { multiple = "multiple" })

                <input type="submit" />
                }


                POST action - use Linq Contains to test against multiple submitted SubSpecialtyIds:



                [HttpPost]
                [ValidateAntiForgeryToken]
                public async Task<ActionResult> Create(CreateDoctorSubSpecialtyViewModel postData) {

                DoctorSubSpecialty allSelectedSubSpecialities = db.DoctorSubSpecialties
                .Where(d => d.DoctorId == postData.DoctorId
                && postData.SubSpecialtyIds.Contains(d.SubSpecialtyId))
                .ToArray();

                // ...
                }




                EDIT @Html.DropDownListFor requires an IEnumerable<SelectListItem> as second parameter.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 27 '18 at 15:24

























                answered Nov 27 '18 at 15:10









                Georg PatscheiderGeorg Patscheider

                7,54211428




                7,54211428
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499734%2fhow-i-can-send-multivalue-to-create-action%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks