Collect data from View to the controller

Asked

Viewed 56 times

1

I would like to know how I can collect a data from View, since I am already using @model System.data.dataset.

@model System.Data.DataSet
@{
   ViewBag.Title = "CadastroPorto";
}

I need to collect the data which will be typed in this part below when clicked on the button:

<section class="content-header">
   <div class="breadcrumb">
   <button type="button" class="btn btn-primary">Cadastrar novo Porto</button>
</div>
<div class="box-body">
        <div class="form-group">
            <label>Nome do Porto</label>
        </div>
</div>
</section>
  • Do you want to take the data without having a form? your code is just that? how do you want to take this data in the controller?

  • 1
  • 2

    @Thiago.Araujo No form is required to traffic data. A good example is if you are using jQuery Ajax or Angular.

  • 1

    In fact @Thiagolunardi, my intention was to understand if he was using only the features of c#, in this case, would use Beginform.

  • 1

    If you plan to use ASP.NET Razor you will need a form. Otherwise, you can do as suggested by colleague Thiago Lunardi and use jQuery or Angular.

  • Good evening, In case how to get the data would be indifferent so I did not put the form. For resolution I used Beginform passed by parameter the input contents. I thank all.

Show 1 more comment

1 answer

2


There’s a lot of weird stuff in the code that showed. A View typed must be the type of an entity of yours. In your example, it should be of the type Porto.

@model `Porto`    
<label>Nome do Porto:</label> @Html.InputFor(porto => porto.NomeDoPorto);

And in the controller that will receive the new port:

[POST][Route("/api/porto")]
public HttpResponseMessage AddPorto (Porto porto)
{
    dbContext.Portos.Add(porto);
    dbContext.SaveChanges();
}
  • I used Beginform. However I will make an example of this as a didactic purpose. Thanks for the help

  • Cool @Alexsandbrush. If it really helps, don’t forget to mark the answer as ideal. Thank you.

Browser other questions tagged

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