How to create dynamically izmenjenu the form of creating the entity?

For example I have two class in the model

public class Book
{
public string Title {get;set;}
public IEnumerable<author> Authors {get;set;}
}

public class Author
{
public string FirstName {get;set;}
public string LastName {get;set;}
}</author>



I want to create a form to create new books, which you can immediately add one by one several of the authors. How do you solve such tasks? Just tell me in what direction to dig.
October 3rd 19 at 03:14
2 answers
October 3rd 19 at 03:16
If I understand correctly the question you are asking, how do you organize on the front-end.
You need to Google in the direction of "js multiselect dropdown".
Here found what I was looking for - Nelle.Muller commented on October 3rd 19 at 03:19
October 3rd 19 at 03:18
well, it is solved in mvc is quite simple.
Google in the direction of the editor(view)template for custom object

in /views/shared/ editortemplates sozdaite daddy

it put the view to edit your model the author
@model YourApp.Models.Author
Html.EditorFor(model => model.FirstName)
Html.EditorFor(model => model.LastName)

in views Create/edit where you want to draw the controls for the fields of the author of the custom of repose above written
Html.EditorFor(model => model.Author)

further, You will have to do
to add the first author at once in the form You need to create view to give an already created object of the class book
// http.get
public ActioResult Create()
{
Book newbook = New Book();
IEnumerable newbook.authors = new IEnumerabe();
newbook.authors.Add(new Author());
return View(newbook);
}

it will remove a bunch of crap

further, you will need a toad a script that adds and removes rows from the dom element of page parent sending it to the server.
I drew my crutch with the help of friends (but I have to study the mvc/html/js), you can look for the required library in nuget'Ah.
or you can your share.

although I have plans already have a writing video course on how to write a complex application in mvc, and the framework in mvc3/4 is utterly dull and it takes a beer for half an hour.
And then, after I write js logic to add and remove runoff from the dom, I have a button that will send a Post request to create the book object with those authors which I have added. There standard can all bind to, or will need js to write the handler of a click, pulling the information about the created object and calling a method of control? - Nelle.Muller commented on October 3rd 19 at 03:21
I have a dom string is added in js a click on the button with a template, also removed the ID of the tr in the table (the first tr in create has id=0) - Nelle.Muller commented on October 3rd 19 at 03:24
I'm interested in how to send and receive all the information entered by the user(title, IEnumerable<author>)</author>.

@using (Html.BeginForm("Create", "Book", FormMethod.Post))
{
 Html.TextBoxFor(m => m.Title);
 Html.EditorFor(model => model.Author)
 <input type="submit" value="Create" )>
}


Is it possible to do to the data when you click on submit automatically bingelis. Or need js to pull all the data that the user entered, and then pass them to controller method? - lane40 commented on October 3rd 19 at 03:27
bind'Yat for you the same @ html.TextboxFor.
and to controller in post method and substituting the appropriate model data and it is perfectly clear, if you create a crud controller based on the existing and describes the model.
tip: follow an introductory course on mvc3/4, You seem to swim in the basics — get familiar with start with simple models, the model for which You just took, is quite complex and requires a slightly different approach, I have decided to translate your project to model such a plan only recently, after a long entertainment with test projects under other goals and objectives - Nelle.Muller commented on October 3rd 19 at 03:30
I know that html.TextboxFor automatically bendida, and Html.EditorFor(model => model.Author) does not automatically bendida after adding for example two authors. I have a js from the html elements to get the user input and pass them to controller method using
$.ajax({ type: "POST", url: url, data: data });
You just do or do you all automatically bendida? - lane40 commented on October 3rd 19 at 03:33
You city stuff.

Create a get method object of class books with two authors, run the project and view the source code of the page create in the browser
hint: pay attention to labels [0], [1] the id the middle of the input's properties s for authors.

then start to think where I went wrong.

hint two.

in the post method "create" to change the accepted arguments
[HttpPost]
public async Task Create(Book book, FormCollection formdata) //book leave as is, to add after the point FormCollection...
after clicking the submit form, when the window opens Studio breakpoints see and compare the properties of the model books and formdata.

hint 3: don't be afraid to experiment - Nelle.Muller commented on October 3rd 19 at 03:36

Find more questions by tags .NETAJAXASP.NETJavaScript