locked
how to save forms value in database RRS feed

  • Question

  • User1928394251 posted

    my view:

    @using (Html.BeginForm("index4", "modiran", FormMethod.Post, null))

    {

    @Html.AntiForgeryToken()


    <label>name=</label>
    <input type="text" />
    <h5> مدیرکل: @Html.RadioButton("modiran_semat", "modirkol") </h5>
    <h5> قایم مقام: @Html.RadioButton("modiran_semat", "ghaemmagham") </h5>
    <h5> معاون خبر: @Html.RadioButton("modiran_semat", "moaven_khabar") </h5>
    <h5> معاون فنی: @Html.RadioButton("modiran_semat", "moaven-fani") </h5>
    <h5>معاون سیما: @Html.RadioButton("modiran_semat", "moaven-sima") </h5>
    <h5> معاون صدا: @Html.RadioButton("modiran_semat", "moaven-seda") </h5>

    <button type="save" value="insert" title="ذخیره"> ذخیره </button>
    }

    Monday, December 24, 2018 10:31 AM

Answers

  • User1520731567 posted

    Hi aabedeni056,

    aabedeni056

    <button type="save" value="insert" title="ذخیره"> ذخیره </button>

    If you use submit form to save data to database,

    I suggest you could modify this line to:

    <button type="submit" value="insert" title="ذخیره"> ذخیره </button>
    

    Only  type="submit",data will be passed by button from view to controller.

    Then the compiler will jump to httppost index4 action,like:

            [HttpPost]
            public ActionResult index4 (XXModel model)
            {
    
                if (ModelState.IsValid)
                {
                    db.xxxTable.Add(model);
                    db.SaveChanges();  //save data to database
                    return RedirectToAction("Index");
                }
    
                return View(model);
            }

    You saw your code:

    <h5> مدیرکل: @Html.c("modiran_semat", "modirkol") </h5>

    do you have a field save modiran_semat value in mode,like:

            public class XXXmodel
            {
               ....
                public string modiran_semat { get; set; }
            }

    If not,please add it to model.

    So, 

    [HttpPost]
    public ActionResult index4 (XXXmodel model)//check if the model contains modiran_semat.(such as; modirkol or ghaemmagham or moaven_khabar...)
    {
    }

    In addition, I suggest you could add a scaffolded item to learn how to implement CRUD in mvc.

    You could follow the steps below:

    Right click on Controllers -> Choose Add -> Choose 'MVC5 Controller with views, using Entity Framework', then Add-> Choose the Model you want to add CRUD to in 'Model class', then Add -> The controller with CRUD function is created.

    Then the  Index,Details,Create,Edit ..pages will be automatically generated.

    This will give you a deeper, more systematic understanding.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 25, 2018 2:33 AM