Member 13649417 Ответов: 1

Я не могу сохранить изменения с помощью функции public actionresult edit ?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class employeeController : Controller
    {
        // GET: employee
        public ActionResult Index()
        {
            var employees = from e in GetEmployeeList()
                            orderby e.ID
                            select e;
            return View(employees);
        }

        // GET: employee/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: employee/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: employee/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: employee/Edit/5
        public ActionResult Edit(int id)
        {
            List<Employee> empList = GetEmployeeList();
            var employee = empList.Single(m => m.ID == id);

            return View(employee);
        }

        // POST: employee/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                List<Employee> empList = GetEmployeeList();
                var employee = empList.Single(m => m.ID == id);

                if (TryUpdateModel(employee))
                {
                    //To Do:- database code
                    return RedirectToAction("Index");
                }
                return View(employee);
            }
            catch
            {
                return View();
            }
        }

        // GET: employee/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: employee/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [NonAction]
        public List<Employee> GetEmployeeList()
        {
            return new List<Employee>{
      new Employee{
         ID = 1,
         Name = "Allan",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 23
      },

      new Employee{
         ID = 2,
         Name = "Carson",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 45
      },

      new Employee{
         ID = 3,
         Name = "Carson",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 37
      },

      new Employee{
         ID = 4,
         Name = "Laura",
         JoiningDate = DateTime.Parse(DateTime.Today.ToString()),
         Age = 26
      },
   };
        }
    }
}


Что я уже пробовал:

i can't save the changes with function public ActionResult Edit ???

1 Ответов

Рейтинг:
0

debasish mishra

public ActionResult Edit(int id, FormCollection collection)
     {
         try
         {
             List<Employee> empList = GetEmployeeList();
             var employee = empList.Single(m => m.ID == id);

             if (TryUpdateModel(employee))
             {
                 //To Do:- database code
                 return RedirectToAction("Index");
             }
             return View(employee);
         }
         catch
         {
             return View();
         }
     }


Ни вы ничего не делаете с коллекцией, ни вы не ловите объект Employee, который модифицирован или готов к обновлению.


Member 13649417

Я вижу, что объект employee - это обновление, но после вызова RedirectToAction("индекс");
все в списке empList очищено

[no name]

Дайте мне знать, почему вы голосуете против?