简体   繁体   English

在MVC中如何在单击按钮时移动到其他动作方法

[英]How to move to other action method on button click in MVC

I made a form where user can enter all details. 我制作了一个表格,用户可以在其中输入所有详细信息。 Like a fill up form. 就像填写表格一样。 After filling up form. 填写表格后。 When user click on Save button it should be automatically move to other action method in same controller. 当用户单击“保存”按钮时,它应自动移至同一控制器中的其他操作方法。 And must show data in GRID VIEW in MVC. 并且必须在MVC的GRID VIEW中显示数据。 Where in grid view user can update and save all data which he entered while filling up the form. 在填写表格时,用户可以在网格视图中的何处更新和保存他输入的所有数据。 I used DB first approach. 我使用数据库优先方法。 And make a view model class. 并制作一个视图模型类。 Here is the code of View Model class. 这是View Model类的代码。

public class ViewModel
{
    [Required(ErrorMessage = "Please Enter Prodcut Name")]
    [DisplayName("Product Name")]
    public string Product_Name { get; set; }
    [Required(ErrorMessage = "Please Choose Category")]
    public int SelectedValue { get; set; }
    [Required(ErrorMessage = "Enter Price")]
    [DisplayName("Enter Price")]
    public decimal Price { get; set; }
    [Required(ErrorMessage = "Choose Picture")]
    [DisplayName("Choose Picture")]
    public string Picture { get; set; }
    [Required(ErrorMessage = "Choos Country")]
    public Nullable<int> Country_ID { get; set; }
    [Required(ErrorMessage = "Choose Type")]
    [DisplayName("Choose Product Type")]
    public string Product_Type { get; set; }
    public SelectList CategoryList { get; set; }
    public SelectList CountryList { get; set; }
    [Required(ErrorMessage = "Select Date")]
    public DateTime Date { get; set; }
}

Controller Code 控制器代码

ProductionEntities DBContext = new ProductionEntities(); 

public ActionResult Index()
{
    ViewModel model = new ViewModel();

    List<tblCategory> CategoryList = DBContext.tblCategories.ToList();
    model.CategoryList = new SelectList(CategoryList, "Category_ID", "Category_Name");

    List<tblCountry> CountryList = DBContext.tblCountries.ToList();
    model.CountryList = new SelectList(CountryList, "Country_ID", "Country_Name");
    return View(model);
}

[HttpPost]
public ActionResult Index(ViewModel model)
{
    //ViewModel v = new ViewModel();
    //if (image1!=null)
    //{
    //    model.Picture = new byte[image1.ContentLength];
    //    image1.InputStream.Read(model.Picture, 0, image1.ContentLength);
    //}

    List<tblCategory> CategoryList = DBContext.tblCategories.ToList();
    model.CategoryList = new SelectList(CategoryList, "Category_ID", "Category_Name");
    List<tblCountry> CountryList = DBContext.tblCountries.ToList();
    model.CountryList = new SelectList(CountryList, "Country_ID", "Country_Name");
    if (!ModelState.IsValid)
    {
        tblProduct product = new tblProduct();
        product.Category_ID = model.SelectedValue;
        product.Country_ID = model.Country_ID;
        product.Price = model.Price;
        product.Product_Name = model.Product_Name;
        product.Date = model.Date;
        product.Picture = model.Picture;
        product.Product_Type = model.Product_Type;
        try
        {
            DBContext.tblProducts.Add(product);
            DBContext.SaveChanges();
        }
        catch  (DbEntityValidationException dbEx)
        {
            foreach (var validationErrors in dbEx.EntityValidationErrors)
            {
                foreach (var validationError in validationErrors.ValidationErrors)
                {
                    System.Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                }
            }
        }
    }
    return View(model);
}

You should follow PRG ( Post-Redirect-Get ) pattern. 您应该遵循PRG( Post-Redirect-Get )模式。 After successfully saving the record, send a redirect response to the browser which will issue a totally new GET request to the action method which renders the tabular data. 成功保存记录后,将重定向响应发送到浏览器,该浏览器将向呈现表格数据的action方法发出全新的GET请求。

DBContext.tblProducts.Add(product);
DBContext.SaveChanges();
return RedirectToAction("List");

and in your List action method you will read the records needed for the tabular data and render it in it's view. 然后在您的List操作方法中,您将读取表格数据所需的记录,并在其视图中进行呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM