如何重新触发使用preventdefault()取消的js事件

时间:2017-10-27 09:56:20

标签: javascript reactjs javascript-events

拥有以下 React 代码:

public ActionResult Index(HttpPostedFileBase uploadfile)
    {
        //List<dataextract> lstStudent = new List<dataextract>();
        if (ModelState.IsValid)
        {
            if (uploadfile != null && uploadfile.ContentLength > 0)
            {
                //ExcelDataReader works on binary excel file
                Stream stream = uploadfile.InputStream;
                //We need to written the Interface.
                IExcelDataReader reader = null;
                if (uploadfile.FileName.EndsWith(".xls"))
                {
                    //reads the excel file with .xls extension
                    reader = ExcelReaderFactory.CreateBinaryReader(stream);
                }
                else if (uploadfile.FileName.EndsWith(".xlsx"))
                {
                    //reads excel file with .xlsx extension
                    reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                }
                else
                {
                    //Shows error if uploaded file is not Excel file
                    ModelState.AddModelError("File", "This file format is not supported");
                    return View();
                }               


                var conf = new ExcelDataSetConfiguration
                {
                    ConfigureDataTable = _ => new ExcelDataTableConfiguration
                    {
                        UseHeaderRow = true
                    }
                };

                DataSet result = reader.AsDataSet(conf);
                DataTable s1 = result.Tables[0];

                reader.Close();                  


                 using (ReadContext db = new ReadContext())
                 {var query = from s in s1.AsEnumerable()

                                select new
                                {

                                    CODE = s.Field<Double>("CODE").ToString(),
                                    Name = s.Field<string>("Name").ToString(),
                                    Group = s.Field<string>("Group").ToString()
                                };
                    var q1 = query.Select(x => new dataextract
                    {

                        CODE = x.CODE,
                        Name = x.Name,
                        Group = x.Group,
                    }).ToList();

                    foreach (var ss in q1)
                     {

                          db.dataext.Add(ss);//error happening at this line 
                     }
                     db.SaveChanges();

                 }

                return View(s1);
            }
        }
        else
        {
            ModelState.AddModelError("File","Please upload your file");
        }
        return View();
  } 

如何使用// foo element <NavLink onClick={(e) => this.sectionSelected(e)} /> //method invoqued sectionSelected(e) { e.preventDefault(); // foo actions // this.setState( ... ) // trigger the click again // e.retrigger() } 重新触发我取消的点击?

寻找不涉及jquery的答案。

1 个答案:

答案 0 :(得分:1)

我认为没有办法以您希望的方式暂停默认事件行为。

您可以包裹NavLink来处理点击并按照默认行为...

const NavLinkWithOnClick = ({
 clickHandler,
 to
}) => (
  <div onClick={clickHandler}>
   <NavLink to={to} />
  </div>
)

...

<NavLinkWithOnClick clickHandler={sectionSelected] to="/wherever" />