How to create an application from scratch in ASP.NET MVC?

Asked

Viewed 737 times

3

I’m studying about the Asp.Net MVC and here I already got a good base of how to start from scratch with Empty, as I haven’t found a good tutorial on how to start.

My problem is this:

I have this form that’s on Jsfiddle and that I have to add this information to a database, that is, I would have to obtain the data -> create a Query of Insert and save in bank.

I’d like to understand where the whole code between the Controller and the Model until the seat is inserted.

If possible with example with codes.

  • 1

    Just complementing @Gypsy’s response, maybe this tutorial can help you understand how to do this. If you have any questions, let me know if I can come up with a response.

  • As soon as possible I will be reading about @Randrade. Vlw too much. More if you want to risk an answer send see.

  • Hello, for you who are starting I suggest you watch the video lessons of Cleyton Ferrari > https://www.youtube.com/user/cleytonferrari/

1 answer

5


I have a better introduction for you.

I’d like to understand where the whole code between the Controller and the Model until the seat is inserted.

Well, you have just answered a part of your own question. I will now go into more technical detail.

Your form has a action, which is the same name as the method in Controller (which, incidentally, we also call Action). For example:

[HttpPost]
public ActionResult MinhaAcao(MeuViewModel viewModel)
{
    ...
}

Your form, in case, would need to be like this:

<form id="form1" action="/MeuController/MinhaAcao">

The field names reflect what the Model or Viewmodel receives. For example, your field:

<input type="text" class="form-control" id="inputNome" name="name" placeholder="Título">

The name his is "name", then the Viewmodel need to have:

public class MeuViewModel
{
    public String name { get; set; } // O campo precisa ter exatamente o mesmo valor do atributo "name" do formulário
    ...
}

The assignment of the sent values is done automatically through a functionality of ASP.NET MVC called Model Binding.

In case, your form is wrong because it has two <input>s with the same "name" (with, in fact, the value "name").

Supposing now that you have corrected your form and made a test to send data to the Controller. If you filled one out Viewmodel and is using Entity Framework, you will have to fill a Model also to enter or update the information in the database, or you may receive a Model filled out the form directly. Some disagree that this is safe, so some currents discourage the use of Models directly into forms. Opinionated speaking, I find it safe because there are ways to check the Model before inserting, using, for example, [Bind].

If you are not using Entity Framework, just read the values of the Model or Viewmodel and put it in the bank any way you want.

I won’t lengthen the answer about the Entity Framework because you don’t need to. If you want to know more, ask another question or express yourself by comments.

  • When you say: Se você não estiver usando Entity Framework, basta ler os valores das propriedades do Model ou ViewModel e inserir no banco da maneira com que quiser. I think to create a class in the model that receives as Parameter Viewmodel and make the insertion, would be ideal? And if I used Ajax would be the same idea?

  • 1

    Not. Models are not good to insert in the database. I would implement a generic repository and pass the class to it. An example is here.

  • 1

    Thank you @Gypsy, sure added me a lot the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.