[SOLVED] When i try to get data from data for edit page it's showing me error

Issue

When I try to get data from data for edit page it’s showing me the following error:

When i try to get data from data for edit page it's showing me error.

So I am building a website where I want to implement a edit banner form when the User can Edit his banner data.

And here is where the problem I raise, when I run the code, it return always as null.

Here is my controller:

[HttpGet]
public ActionResult EditBanner(int id)
{
    var abd = (from a in _db.Banner
                where id == a.BannerId
                select a).FirstOrDefault();

    return View(abd);
}

[HttpPost]
public ActionResult UpdateBannerDatas(BannerViewModels bnrupdate)
{
    var bnrdata = _db.Banner.Where(x => x.BannerId == bnrupdate.BannerId).FirstOrDefault();
    if (bnrdata != null)
    {
        bnrdata.BannerTitle = bnrupdate.BannerTitle;
        bnrdata.BannerUrl = bnrupdate.BannerUrl;
        bnrdata.BannerIndex = bnrupdate.BannerIndex;
        bnrdata.BannerDescription = bnrupdate.BannerDescription;
        bnrdata.BannerIndex = bnrupdate.BannerIndex;

        _db.SaveChanges();

    }
    return Redirect("BannerDetails");
}

Here is my viewmodel:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SZGMC.Web.Models;

namespace SZGMC.Web.Areas.Admin.ViewModels
{
    public class BannerViewModels
    {
        public int BannerId { get; set; }
        public string BannerTitle { get; set; }
        public string BannerDescription { get; set; }
        public string BannerImg { get; set; }
        public IFormFile BannerImg1 { get; set; }
        public string BannerUrl { get; set; }
        public string BannerIndex { get; set; }
        public int? BMasterId { get; set; }
        public byte? IsDeleted { get; set; }

        public virtual BannerMaster BMaster { get; set; }
    }
}

Here is my model:

using System;
using System.Collections.Generic;

// Code scaffolded by EF Core assumes nullable reference types (NRTs) are not used or disabled.
// If you have enabled NRTs for your project, then un-comment the following line:
// #nullable disable

namespace SZGMC.Web.Models
{
    public partial class Banner
    {
        public int BannerId { get; set; }
        public string BannerTitle { get; set; }
        public string BannerDescription { get; set; }
        public string BannerImg { get; set; }
        public string BannerUrl { get; set; }
        public string BannerIndex { get; set; }
        public int? BMasterId { get; set; }
        public byte? IsDeleted { get; set; }

        public virtual BannerMaster BMaster { get; set; }
    }
}

Here is my view:

<form asp-controller="Home" asp-action="UpdateBannerDatas" enctype="multipart/form-data" method="post">

    <div id="main-content">
        <div class="container-fluid">
            <!-- Page header section  -->
            <div class="block-header">
                <div class="row clearfix">
                    <div class="col-lg-6 col-md-5 col-sm-12">
                        <h1>Hi, Welcomeback!</h1>
                        <span>You can edit banner here</span>
                    </div>
                    <div class="col-xl-6 col-md-7 col-sm-12 text-md-right">
                        <div class="d-flex align-items-center justify-content-md-end mt-4 mt-md-0 flex-wrap vivify pullUp delay-550">
                            <div class="mb-3 mb-xl-0 ">
                                <a asp-action="BannerDetails" class="btn btn-dark">Banner List</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row clearfix">
                <div class="col-12">
                    <div class="card">
                        <div class="body">
                            <div class="header">
                                <h2><strong>Enter Banner Details</strong></h2>
                            </div>
                            <br />
                            <input asp-for="BannerId" type="hidden" />
                            <div class="row">
                                <div class="col-12">
                                    <div class="form-group c_form_group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text"></span>
                                            </div>
                                            <input type="text" class="form-control" asp-for="BannerTitle" placeholder="Banner Title" aria-label="bannertitle" aria-describedby="basic-addon1">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-6">
                                    <div class="form-group c_form_group">
                                        <label>Banner Description</label>
                                        <div class="input-group">
                                            <textarea class="form-control" asp-for="BannerDescription" aria-label="Banner Description" rows="6"></textarea>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="drop-zone">
                                        <span class="drop-zone__prompt">Drop file here or click to upload</span>
                                        <input type="file" asp-for="BannerImg1" name="myFile" class="drop-zone__input" accept="image/*" data-allowed-file-extensions='["jpg", "png" , "jpeg"]' required>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-6">
                                    <div class="form-group c_form_group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text"></span>
                                            </div>
                                            <input type="text" class="form-control" asp-for="BannerIndex" placeholder="Banner Index" aria-label="bannerindex" aria-describedby="basic-addon1">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-6">
                                    <div class="form-group c_form_group">
                                        <div class="input-group">
                                            <div class="input-group-prepend">
                                                <span class="input-group-text"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="mb-2" align="center">
                                <button type="submit" class="btn btn-success btn-round">Edit Banner</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

When i try To Edit the data in the browser it Throw exception, in database i have value but when i tried to fetch data its showing error.

thank you in advance to everybody for any help or advice.

Solution

According to the error message it is necessary to pass the BannerViewModels instance instead of the Banner entity:

public ActionResult EditBanner(int id)
{
    var abd = _db.Banner.Where(a => a.BannerId == id)
        .Select(b => new BannerViewModels()
        {
            BannerId = b.BannerId,
            BannerTitle = b.BannerTitle,
            BannerDescription = b.BannerDescription,
            BannerImg = b.BannerImg,
            BannerUrl = b.BannerUrl,
            BannerIndex = b.BannerIndex,
            BMasterId = b.BMasterId,
            IsDeleted = b.IsDeleted
        })
        .FirstOrDefault();

    return View(abd);
}

Passing an entity to the view as a data model is bad idea.

See the following post: Why it’s not a good idea to pass entities as Models in MVC?

Answered By – Jackdaw

Answer Checked By – Marie Seifert (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *