Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

C# MVC System.Web.Mvc.WebViewPage<TModel>.Model.get returned null

I am encountering a strange issue. I have a dropdown list for countries in a form that is populated from the Controller.

public ActionResult Add(Guid id)
    {
        try
        {

            GeneralEntities generalEntities = new GeneralEntities();
            List<SelectListItem> Country = new List<SelectListItem>();
            AddAddressViewModel casModel = AddAddressViewModel.GetCompanyId(id, db);
            List<Countries> Acountry = generalEntities.Countries.ToList();
            Acountry.ForEach(x =>
            {
                Country.Add(new SelectListItem { Text = x.CountryName, Value = x.CountryId.ToString() });
            });
            casModel.Countries = Country;

            return View(casModel);
        }
        catch (Exception)
        {

            return View();
        }
    }

The add view is selected to add an address to an Account. I created a new Account and then went to add an address to it and it is giving me the error for the list of countries. I set breakpoints in the Controller and the Model and get a list of countries. But the view is erroring that the list is null.

When I select this on an existing account there is no issues, it comes up. This was working before for a long time. I am not sure what has changed.

Adding an Account only puts a record in the database with the name and of course the Id and UID, I use the UID, which does exist. The countries table has nothing to do with the Account. The add view just shows a list of Countries, states, cities. that depend on each other to populate except Countries, it is stand alone. If I Comment out the dropdown for the Countries in the View, the page loads.

Any ideas as to why it is doing this? Any help would be appreciated.

UPDATE:

I found my issue or what actually caused it. I am still confused as to why it would cause a stand alone list to not populate. My ViewModel Had a join on a SQLView table that Lists Addresses. I am assuming that since there was no addresses in this table related to the new account it errored out. Why it errored on the countries List, I have no Idea. Below is the ViewModel I pointed to.

public class AddAddressViewModel
{
    public static AddAddressViewModel GetCompanyId(Guid id, GeneralEntities db)
    {
        var qCust = from ad in db.Addresses

                    join adv in db.AddressView on ad.AddressId equals adv.AddressId
                    join cus in db.CompanyNames on ad.CompanyId equals cus.CompanyId
                    where (adv.CompanyUI == id)
                    select new AddAddressViewModel()
                    {
                        CompanyUI = adv.CompanyUI,
                        CompanyId = cus.CompanyId
                    };

        var result = qCust.SingleOrDefault();

        return result;
    }

 XXXX-- Enitities List --XXXX
}

I had another ViewModel that was created and now point to:

public class AddAddressesViewModel
{
    public static AddAddressesViewModel GetCompanyId(Guid id, GeneralEntities db)
    {
        var qCust = from ad in db.CompanyNames

                    join cus in db.CompanyNames on ad.CompanyUI equals cus.CompanyUI
                    where (cus.CompanyUI == id)
                    select new AddAddressesViewModel()
                    {
                        CompanyId = cus.CompanyId,
                        CompanyUI = cus.CompanyUI
                    };

        var result = qCust.SingleOrDefault();

        return result;
    }

  XXX-- Entities --XXX
}

This does not look in the Addresses and it seems to be working.

If anyone can tell me why this had anything to do with a stand alone Countries list it would be much appreciated.

I am also not sure why this ViewModel joins to itself, I was not the one who created it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...