MVC model with a property for related models












0















Imagine there is a model defined as:



public class Book
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<Guid> RelatedBooks { get; set; } // List<Book> would be more ideal.
}


I'm trying to add a field in both the Create and Edit views whereby a user will be able to search for related items, in this example by name, and then add them to the new/current item as a related item.



I think this might be possible with a partial view + jQuery, where the partial view acts a search box and returns a list of items. Then with some buttons and jQuery magic these could be added to the current model upon submission of the parent form.



I'm not against using jQuery if it's the only option, but I was wondering if there were any alternatives?



If this is too vague I'm happy to add some more specific details, I've tried search for similar questions but I'm either using the wrong search terms or there aren't any.



Any help would be really appreciated - I'd rather not go down the jQuery + partial view route if there is a cleaner alternative.










share|improve this question



























    0















    Imagine there is a model defined as:



    public class Book
    {
    public Guid Id { get; set; }
    public string Name { get; set; }
    public List<Guid> RelatedBooks { get; set; } // List<Book> would be more ideal.
    }


    I'm trying to add a field in both the Create and Edit views whereby a user will be able to search for related items, in this example by name, and then add them to the new/current item as a related item.



    I think this might be possible with a partial view + jQuery, where the partial view acts a search box and returns a list of items. Then with some buttons and jQuery magic these could be added to the current model upon submission of the parent form.



    I'm not against using jQuery if it's the only option, but I was wondering if there were any alternatives?



    If this is too vague I'm happy to add some more specific details, I've tried search for similar questions but I'm either using the wrong search terms or there aren't any.



    Any help would be really appreciated - I'd rather not go down the jQuery + partial view route if there is a cleaner alternative.










    share|improve this question

























      0












      0








      0








      Imagine there is a model defined as:



      public class Book
      {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public List<Guid> RelatedBooks { get; set; } // List<Book> would be more ideal.
      }


      I'm trying to add a field in both the Create and Edit views whereby a user will be able to search for related items, in this example by name, and then add them to the new/current item as a related item.



      I think this might be possible with a partial view + jQuery, where the partial view acts a search box and returns a list of items. Then with some buttons and jQuery magic these could be added to the current model upon submission of the parent form.



      I'm not against using jQuery if it's the only option, but I was wondering if there were any alternatives?



      If this is too vague I'm happy to add some more specific details, I've tried search for similar questions but I'm either using the wrong search terms or there aren't any.



      Any help would be really appreciated - I'd rather not go down the jQuery + partial view route if there is a cleaner alternative.










      share|improve this question














      Imagine there is a model defined as:



      public class Book
      {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public List<Guid> RelatedBooks { get; set; } // List<Book> would be more ideal.
      }


      I'm trying to add a field in both the Create and Edit views whereby a user will be able to search for related items, in this example by name, and then add them to the new/current item as a related item.



      I think this might be possible with a partial view + jQuery, where the partial view acts a search box and returns a list of items. Then with some buttons and jQuery magic these could be added to the current model upon submission of the parent form.



      I'm not against using jQuery if it's the only option, but I was wondering if there were any alternatives?



      If this is too vague I'm happy to add some more specific details, I've tried search for similar questions but I'm either using the wrong search terms or there aren't any.



      Any help would be really appreciated - I'd rather not go down the jQuery + partial view route if there is a cleaner alternative.







      c# .net model-view-controller asp.net-mvc-5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 13:19









      CallumCallum

      8910




      8910
























          1 Answer
          1






          active

          oldest

          votes


















          0














          First off, you need to create a view-model for what you're trying to achieve that will hold both the list of related books, but also a list of all books we can select.



          public class CreateBookViewModel {
          public Guid Id { get; set; }
          public string Name { get; set; }
          public List<Guid> RelatedBooks { get; set; }

          public List<Book> AllBooks { get; set; }
          }


          Populate AllBooks in the Controller and return the view model. Then you could create some magic, maybe with jQuery, to have a select list with all books, that when selected are added to the list of related books by Id. At least you wouldn't need partial views, if you don't want any more functionality than just selecting one or more books.






          share|improve this answer
























          • Ah yeah, the model in question was hypothetical (I do use view models in my actual implementation) - I have considered the above, but it prevents the user from being able to search through all of the books, and also means loading every single book every time they may want to create or edit one which eventually (with lots of books) will not be good for performance. Thanks for the input though, really appreciate it, and if I can't find an alternative, a proof of concept like the above will be a good starting point.

            – Callum
            Nov 22 '18 at 13:57











          • @Callum Will edit my answer when I have time then, because then I definitely think you should use jQuery (or any other Javascript framework for that matter) to have an autocomplete searchbox on the create/edit page, which requests book from an AJAX endpoint on some controller based on the search condition.

            – JLe
            Nov 22 '18 at 13:59











          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%2f53431910%2fmvc-model-with-a-property-for-related-models%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









          0














          First off, you need to create a view-model for what you're trying to achieve that will hold both the list of related books, but also a list of all books we can select.



          public class CreateBookViewModel {
          public Guid Id { get; set; }
          public string Name { get; set; }
          public List<Guid> RelatedBooks { get; set; }

          public List<Book> AllBooks { get; set; }
          }


          Populate AllBooks in the Controller and return the view model. Then you could create some magic, maybe with jQuery, to have a select list with all books, that when selected are added to the list of related books by Id. At least you wouldn't need partial views, if you don't want any more functionality than just selecting one or more books.






          share|improve this answer
























          • Ah yeah, the model in question was hypothetical (I do use view models in my actual implementation) - I have considered the above, but it prevents the user from being able to search through all of the books, and also means loading every single book every time they may want to create or edit one which eventually (with lots of books) will not be good for performance. Thanks for the input though, really appreciate it, and if I can't find an alternative, a proof of concept like the above will be a good starting point.

            – Callum
            Nov 22 '18 at 13:57











          • @Callum Will edit my answer when I have time then, because then I definitely think you should use jQuery (or any other Javascript framework for that matter) to have an autocomplete searchbox on the create/edit page, which requests book from an AJAX endpoint on some controller based on the search condition.

            – JLe
            Nov 22 '18 at 13:59
















          0














          First off, you need to create a view-model for what you're trying to achieve that will hold both the list of related books, but also a list of all books we can select.



          public class CreateBookViewModel {
          public Guid Id { get; set; }
          public string Name { get; set; }
          public List<Guid> RelatedBooks { get; set; }

          public List<Book> AllBooks { get; set; }
          }


          Populate AllBooks in the Controller and return the view model. Then you could create some magic, maybe with jQuery, to have a select list with all books, that when selected are added to the list of related books by Id. At least you wouldn't need partial views, if you don't want any more functionality than just selecting one or more books.






          share|improve this answer
























          • Ah yeah, the model in question was hypothetical (I do use view models in my actual implementation) - I have considered the above, but it prevents the user from being able to search through all of the books, and also means loading every single book every time they may want to create or edit one which eventually (with lots of books) will not be good for performance. Thanks for the input though, really appreciate it, and if I can't find an alternative, a proof of concept like the above will be a good starting point.

            – Callum
            Nov 22 '18 at 13:57











          • @Callum Will edit my answer when I have time then, because then I definitely think you should use jQuery (or any other Javascript framework for that matter) to have an autocomplete searchbox on the create/edit page, which requests book from an AJAX endpoint on some controller based on the search condition.

            – JLe
            Nov 22 '18 at 13:59














          0












          0








          0







          First off, you need to create a view-model for what you're trying to achieve that will hold both the list of related books, but also a list of all books we can select.



          public class CreateBookViewModel {
          public Guid Id { get; set; }
          public string Name { get; set; }
          public List<Guid> RelatedBooks { get; set; }

          public List<Book> AllBooks { get; set; }
          }


          Populate AllBooks in the Controller and return the view model. Then you could create some magic, maybe with jQuery, to have a select list with all books, that when selected are added to the list of related books by Id. At least you wouldn't need partial views, if you don't want any more functionality than just selecting one or more books.






          share|improve this answer













          First off, you need to create a view-model for what you're trying to achieve that will hold both the list of related books, but also a list of all books we can select.



          public class CreateBookViewModel {
          public Guid Id { get; set; }
          public string Name { get; set; }
          public List<Guid> RelatedBooks { get; set; }

          public List<Book> AllBooks { get; set; }
          }


          Populate AllBooks in the Controller and return the view model. Then you could create some magic, maybe with jQuery, to have a select list with all books, that when selected are added to the list of related books by Id. At least you wouldn't need partial views, if you don't want any more functionality than just selecting one or more books.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 13:49









          JLeJLe

          2,45831328




          2,45831328













          • Ah yeah, the model in question was hypothetical (I do use view models in my actual implementation) - I have considered the above, but it prevents the user from being able to search through all of the books, and also means loading every single book every time they may want to create or edit one which eventually (with lots of books) will not be good for performance. Thanks for the input though, really appreciate it, and if I can't find an alternative, a proof of concept like the above will be a good starting point.

            – Callum
            Nov 22 '18 at 13:57











          • @Callum Will edit my answer when I have time then, because then I definitely think you should use jQuery (or any other Javascript framework for that matter) to have an autocomplete searchbox on the create/edit page, which requests book from an AJAX endpoint on some controller based on the search condition.

            – JLe
            Nov 22 '18 at 13:59



















          • Ah yeah, the model in question was hypothetical (I do use view models in my actual implementation) - I have considered the above, but it prevents the user from being able to search through all of the books, and also means loading every single book every time they may want to create or edit one which eventually (with lots of books) will not be good for performance. Thanks for the input though, really appreciate it, and if I can't find an alternative, a proof of concept like the above will be a good starting point.

            – Callum
            Nov 22 '18 at 13:57











          • @Callum Will edit my answer when I have time then, because then I definitely think you should use jQuery (or any other Javascript framework for that matter) to have an autocomplete searchbox on the create/edit page, which requests book from an AJAX endpoint on some controller based on the search condition.

            – JLe
            Nov 22 '18 at 13:59

















          Ah yeah, the model in question was hypothetical (I do use view models in my actual implementation) - I have considered the above, but it prevents the user from being able to search through all of the books, and also means loading every single book every time they may want to create or edit one which eventually (with lots of books) will not be good for performance. Thanks for the input though, really appreciate it, and if I can't find an alternative, a proof of concept like the above will be a good starting point.

          – Callum
          Nov 22 '18 at 13:57





          Ah yeah, the model in question was hypothetical (I do use view models in my actual implementation) - I have considered the above, but it prevents the user from being able to search through all of the books, and also means loading every single book every time they may want to create or edit one which eventually (with lots of books) will not be good for performance. Thanks for the input though, really appreciate it, and if I can't find an alternative, a proof of concept like the above will be a good starting point.

          – Callum
          Nov 22 '18 at 13:57













          @Callum Will edit my answer when I have time then, because then I definitely think you should use jQuery (or any other Javascript framework for that matter) to have an autocomplete searchbox on the create/edit page, which requests book from an AJAX endpoint on some controller based on the search condition.

          – JLe
          Nov 22 '18 at 13:59





          @Callum Will edit my answer when I have time then, because then I definitely think you should use jQuery (or any other Javascript framework for that matter) to have an autocomplete searchbox on the create/edit page, which requests book from an AJAX endpoint on some controller based on the search condition.

          – JLe
          Nov 22 '18 at 13:59


















          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%2f53431910%2fmvc-model-with-a-property-for-related-models%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python