0

I am having problems inserting values (strings) into a database in ASP.NET MVC / C#. I am stuck because I don't know what to do.

@using (Html.BeginForm("Index", "Upload", FormMethod.Post))
            {
                @Html.Label("Name:")
                @Html.TextBox("name", null, new { @class = "form-control"})

                @Html.Label("Surname:")
                @Html.TextBox("surname", null, new { @class = "form-control" })

                @Html.Label("Email:")
                @Html.TextBox("email", null, new { @class = "form-control" })

                @Html.Label("Description:")
                @Html.TextArea("description", null, new { @class = "form-control" })

                <br/>
                <input type="submit" class="btn btn-primary" value="Send" />
            }

My model is empty ... :-(

namespace GikStore.Models {
   public class ContactModels
   {
   }
}

Do I have to do anything with the controller?

4
  • Your controller is supposed to send the model to the view. Seems you are not doing so, based on "Do I have to do anything with the controller?" What does the Upload action in the Index Controller do, if anything ? It is it that needs to send the populated model to the view for display. Commented May 22, 2016 at 11:57
  • 1
    What is your model? What is your controller method that you post to? Commented May 22, 2016 at 11:59
  • @Krasimir answered correctly and little bit more if you use TextBoxFor(x=>x.Name) & LabelFor(x=>x.Name) in your view instead of TextBox & Label so it will be more help full for you Commented Oct 19, 2017 at 5:30
  • convert TextBox to TextBoxFor Commented Nov 8, 2017 at 7:00

4 Answers 4

2

In the UploadController you should have something like this:

    [HttpGet]
    public ActionResult Index()
    {
        return View(new ContactModels());
    }
    [HttpPost, Admin]
    public ActionResult Index(ContactModels model)
    {

        if (!ModelState.IsValid)
        {
            return View(model);
        }

        //Call a service that saves to database

        return RedirectToAction("ListOfContacts");
    }

The RedirectToAction("ListOfContacts"); is just an example. You can redirect to any Action and Controller.

Your Model should look something like this:

public class ContactModels
{
    public string name { get; set; }
    public string surname { get; set; }
    public string email { get; set; }
    public string description { get; set; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I checked my returning values, and they work. Now I am missing the part where I upload it (them). I prosume I do that with with my Controller
Yes. Where I wrote the comment //Call a service that saves to database you should insert some code that should do the saving. Depends on how you connect to your database - Entity Framework, Ado.NET or NHibernate. Preferably you do not want to work with these connections directly in the controller. Instead you should write a service (for example ContactService) that should handle the database connection and also the different operations. Then the ContactService can have Upload, Delete, Get methods and others and you just call that service method in your Controller Action.
0

First Create a Model class

 public class Contact
{
 public string name { get; set; }
 public string surname { get; set; }
 public string email { get; set; }
 public string description { get; set; }
}

Change View

   @model.Contact
   @using (Html.BeginForm("Index", "Upload", FormMethod.Post))
        {
            @Html.Label("Name:")
            @Html.TextBoxFor(m=>m.name, new { @class = "form-control"})

            @Html.Label("Surname:")
            @Html.TextBoxFor(m=>m.surname, new { @class = "form-control" })

            @Html.Label("Email:")
            @Html.TextBoxFor(m=>m.email, new { @class = "form-control" })

            @Html.Label("Description:")
            @Html.TextAreaFor(m=>m.description, new { @class = "form-control" })

            <br/>
            <input type="submit" class="btn btn-primary" value="Send" />
        }

Then add a post method in controller to save data to database

[httpPost]
public ActionResult Index(Contact model)
   {

    if (!ModelState.IsValid)
    {
         //write  the code  that saves to database
    }



    return View();
}

Comments

0

In Model

public class Contact {
        public string name { get; set; }
        public string surname { get; set; }
        [EmailAddress(ErrorMessage = "Invalid Email Address")]
        public string email { get; set; }
        public string description{ get; set; }    
    }

In View (TextBox => TextBoxFor )

 @model ProjectName.Models.Contact
    @using (Html.BeginForm("Index", "Upload", FormMethod.Post))
                {
                    @Html.Label("Name:")
                    @Html.TextBoxFor("name", null, new { @class = "form-control"})

                    @Html.Label("Surname:")
                    @Html.TextBoxFor("surname", null, new { @class = "form-control" })

                    @Html.Label("Email:")
                    @Html.TextBoxFor("email", null, new { @class = "form-control" })

                    @Html.Label("Description:")
                    @Html.TextBoxFor("description", null, new { @class = "form-control" })

                    <br/>
                    <input type="submit" class="btn btn-primary" value="Send" />
                }

In Controller

[httpPost]
public ActionResult Index(Contact model)
   {

    if (!ModelState.IsValid)
    {

    }      

    return View();
}

Comments

0

Create a model class. Say

public class Contact{
    public string Name { get; set; }
    public string Phone { get; set; }
}

The following that import the model to your view after build and then in controller assign a [HttpPost] to map. Your controller can then work on the data provided from the view

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.