Pass the forms data from view to controller and save to database in asp.net mvc3

2.1k Views Asked by At

I have a table in my database and it has a column named title in nvarchar datatype. I want to save the title that user enters in a textbox to table. In my view I used

@using (Html.BeginForm("savetodb", "Advertise", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
    @Html.TextBoxFor(m => m.AdTitle)
} 

This view is strongly typed view from a ViewModel that has this property

public string AdTitle { get; set; }

I know how to save data from controller to database but I don't know how can I access data in my controller(in savetodb() function). Can anybody help me please? Thanks

2

There are 2 best solutions below

1
On BEST ANSWER
public ActionResult SaveToDB(YourViewModel model)
{
    // Do what you want with AdTitle here.
    SaveInDB(model.AdTitle);
}
1
On

View Model coud be one way but in this case i think it`s better to use auto model binding of MVC 3.so if u define a string parameter with the name of AdTitle.

 public ActionResult SaveToDB(String AdTitle)
{
    // Do what you want with AdTitle here.
    SaveInDB(AdTitle);
}