Checkboxlistfor with error

Asked

Viewed 673 times

3

Description: Error when compiling a resource needed to fulfill this request. Examine the error-specific details and modify the source code appropriately.

Compiler Error Message: CS1061: 'System.Web.Mvc.Htmlhelper' does not contain a definition for 'Checkboxlistfor' and no extension method 'Checkboxlistfor' accepts that a first argument of type 'System.Web.Mvc.Htmlhelper' is found (You are not using an Assembly guideline or reference?)

I already downloaded his Assembly, I even have in the references, but he makes this mistake.

And it would have to put together with this checkboxlist, radio Buttons and text inputs ?

Here is the view code:

@using MvcCheckBoxList.Model
@model MvcCheckBoxListForApp.Models.FruitViewModel

@{
  ViewBag.Title = "Home Page";
}

<section class="checkBoxListFor">
  <p>
     <label>Please Select Fruits:</label>
     @using (Html.BeginForm("Index", "Home", FormMethod.Post))
     {
         @Html.CheckBoxListFor(model => model.PostedFruits.FruitIds,
                               model => model.AvailableFruits,
                               fruit => fruit.Id,
                               fruit => fruit.Name,
                               model => model.SelectedFruits,
                               Position.Horizontal)
         <input class="green" type="submit"
                value="POST to Controller" />
    }
    </p>
</section>
  • Downloaded how? Installed the package via Nuget?

  • Exactly, I tried both ways, both by the console, both by the Nuget.

2 answers

1


Example:

1 - View

@model WebApplication2.Models.BaseModel
@{
    ViewBag.Title = "Listas";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Listas</h2>
@using (Html.BeginForm())
{
    @Html.CheckBoxListFor(
        p => p.BaseSend.Send,
        p => p.BaseAll,
        p => p.Id,
        p => p.Name,
        b => b.BaseSelected            
    )
    <button type="submit">Enviar</button>
}

2 - Classes

public class Base
{       
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }
    public object Tags { get; set; }
    
}

public class BaseModel
{
    public IEnumerable<Base> BaseAll { get; set; }
    public IEnumerable<Base> BaseSelected { get; set; }
    public BaseSend BaseSend { get; set; }
}

public class BaseSend
{
    public string[] Send { get; set; }
}

3 - Controller (Method: Lists (GET and POST))

public ActionResult Listas()
{
    BaseModel baseModel = new BaseModel();

    baseModel.BaseAll = new List<Base>(){
        new Base() { Id = 1, Name="Produto 1", IsSelected = true, Tags="Produtos"},
        new Base()  {Id = 2, Name="Produto 2", IsSelected = false, Tags="Produtos"}
    };

    baseModel.BaseSelected = new List<Base>(){
         new Base() { Id = 1, Name="Produto 1", IsSelected = true, Tags="Produtos"},
    };

    return View(baseModel);
}


[HttpPost]
public ActionResult Listas(BaseSend BaseSend)
{    
    return RedirectToAction("Listas");
}

4 - Generated Html

inserir a descrição da imagem aqui


5 - Recovering Values inserir a descrição da imagem aqui


All copyright to the website How to Use Checkboxlistfor With ASP.NET MVC 4?

  • I made mine even based on this link you mentioned ! But the way you made it was much clearer for me ! But for example, I could put along with these check boxes, some radio Buttons and attach to check boxes a text input as well ? All in one form, and save in the database so that I can then list, edit, add and remove ? @Fccdias will have how you put the Razor code also to generate this HTML ?

  • More can be put input of all kinds, with model or simple more believe you better open a new question if not disfocus of this!!!

  • 1

    Got it, I’m gonna ask you another question related to that too !

1

According to the documentation:

You need to add the following reference to your view first:

@using MvcCheckBoxList.Model
  • I made the change and it keeps making mistakes.... Which is because it’s the same as here http://www.codeproject.com/Tips/613785/How-to-Use-CheckBoxListFor-With-ASP-NET-MVC

  • The project is not finding the Extension. Positioning the course over @Html.CheckBoxListFor and pressing F12 right on top of the word CheckBoxListFor, what happens?

  • "Cannot navigate to 'Checkboxlistfor'". Very strange because the reference of it is here in the project.

  • Visual Studio is getting lost in the reference. Close Visual Studio and open it again.

  • Yeah, it worked now, but I’m having another error: Exception Details: System.Entrypointnotfoundexception: The entry point was not found.. Strange, what can it be ?

  • This is when the used DLL has two different versions, and the VS is getting lost in which it should use.

  • Strange, because I only have a dll installed. What should I do ? Delete the project and create again ?

  • It’s an idea. I would just start a new project.

  • Now it worked! I found out what it was. When you install it from the version conflict, but just go to the navigation bar up there, open the prospectve Error List and click on the Warning that gives, VS sets everything right !

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.