-1

i am having a problem running this project : i have a table for my products and in the same page i can to press on "add" button and i will have a small window to add my new product .. this is my controller:

public class HomeController : Controller
{
    CountingEntities hsb = new CountingEntities();

    public ActionResult ProductTable()
    {

        var product = hsb.HesapTemels.ToList();

        List<HesapTemel> yardimci = hsb.HesapTemels.ToList();
        ViewBag.Funds = yardimci;
        return View(product);
    }
    [HttpPost]
    public ActionResult ProductTable(HesapTemel p)
    {
        try
        {
            HesapTemel _he = new HesapTemel();

            _he.HesapAdi = p.HesapAdi;
            _he.HesapKodu = p.HesapKodu;
            _he.HesaptipiID = p.HesaptipiID;
            hsb.HesapTemels.Add(_he);
            hsb.SaveChanges();
            return RedirectToAction("HesapTable", "Home");
          }

        catch (Exception ex)
        {
            throw new Exception("an error happened" + ex.Message);
        }
    }

and this is my view:

<div>
<a class="btn btn-success" data-toggle="modal" data-target=".bootstrapmodal">
    <span class="glyphicon glyphicon-eye-open">
    </span>Add
</a>
<div class="modal fade bootstrapmodal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"></div>
            <div class="modal-body">
                @Html.Partial("_partial.cshtml");
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>
<div>
    <table style="width: 100%;">
        <thead>
            <tr>
                <th>id</th>
                <th>hesap kodu</th>
                <th>hesap adı</th>
                <th>hesap tipi</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in ViewBag.Funds)
        {
                <tr>
                    <td>@item.ID</td>
                    <td>@item.HesapKodu</td>
                    <td>@item.HesapAdi</td>
                    <td>@item.Hesaptipi.Harf</td>
                </tr>
            }
        </tbody>
    </table>
</div>

and this is my partial view :

@model WebApplication7.Models.HesapTemel
@using (Html.BeginForm("ProductTable", "Home", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" }))
{
<div class="row">
    <div class="form-horizontal">
        <div class="form-group">
            <div class="col-md-12">
                <label class="col-sm-2 control-label">Hesap kodu</label>
                <div class="col-sm-10">
                    @Html.TextBoxFor(m => m.HesapKodu, new { @class = "form-control" })
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-12">
                <label class="col-sm-2 control-label">Hesap adı</label>
                <div class="col-sm-10">
                    @Html.TextAreaFor(m => m.HesapAdi, new { @class = "form-control" })
                </div>
            </div>
        </div>
    </div>
</div>
<div class="panel-footer">
    <div class="row">
        <div class="col-sm-9 col-sm-offset-3">
            <div class="btn-toolbar pull-right">
                <input type="submit" value="save" class="btn btn-primary" />
            </div>
        </div>
    </div>
</div>

}

when i am running this solution i have this error : The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[WebApplication7.Models.HesapTemel]', but this dictionary requires a model item of type 'WebApplication7.Models.HesapTemel'.

  • Which line of code shows the error? What is the model type for main view? – Chetan Jul 11 '17 at 14:42
  • its not showing any line error ! .. model type is the same with main view...the project is working when i am deleting my partial view and write the same code but as a main view .. but i want to make the "add product" in the same page without going to another link – Eyad Almansour Jul 11 '17 at 14:56

1 Answers1

0

Your ProductTable action method is passing a list of HesapTemels objects to the view and i am assuming your main view is strongly typed to a collection of this same HesapTemels objects. Your partial view is strongly typed to a single instance of HesapTemel. In your main view, you are calling the Partial method without explcitly passing a model to it. If you do not pass a model , It will use the model passed to the parent view from this partial is called, hence using the collection of HesapTemel objects which was passed to the main view. The partial is expecting a single HesapTemel object to be passed to it and now it is getting a collection of it and that is the reason you are seeing the error.

The solution is to pass the correct data. Since the partial is expecting a single object of HesapTemel, you may loop through the collection in your main view and call the partial in the loop and pass each item of the looped collection.

Shyju
  • 214,206
  • 104
  • 411
  • 497