asp.net-mvcasp.net-mvc-4editorformodel

mvc reach editorfor from controller by its name


hello everyone I am trying to reach EditorFor by giving a name to it. I gave a name like this

@Html.EditorFor(model => model.name,new {name = "sizeName"})

and I am trying to get it from controller

public ActionResult EditSize(int id,string sizeName)
    {
        Repository<SizeList> _rs = new Repository<SizeList>();
        SizeList _sizeList = _rs.Find(a => a.id == id);

        _sizeList.name = sizeName;
        _rs.Save();

        return RedirectToAction("Size");
    }

but it didnt get the sizeName what can I do for this thank you ?


Solution

  • because with @Html.EditorFor() you already set name by passing model to this helper. This helper generate HTML attribute with name="name".

    So, in your controller method you should have:

    public ActionResult EditSize(string name,int id)
        {
            Repository<SizeList> _rs = new Repository<SizeList>();
            SizeList _sizeList = _rs.Find(a => a.id == id);
    
            _sizeList.name = name;
            _rs.Save();
    
            return RedirectToAction("Size");
        }
    

    and your helper should looks like:

    @Html.EditorFor(model => model.name)