0

I always getting this error in my dropdownlistfor:

Value cannot be null.
Parameter name: items

View code:

@Html.DropDownListFor(model => model.SiteId, new SelectList(ViewBag.ListSiteClass, "SiteId", "SiteName"), "", new { @onchange = "GetSiteid(this.value);" })

Controller Code:

 [HttpPost]
        public ActionResult Create(ServiceDTO ms, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                int sid = ms.SiteId;
                ServiceDAO.Insert(sid);
            }
            else
            {
                return View();
            }

        }

In loading View:

public ActionResult Create()
        {
            List<SiteDTO> LSSiteNameList = SiteDAO.GetSiteNameList(User.Identity.Name);
            ViewBag.ListSiteClass = LSSiteNameList;

            var service = new ServiceDTO();
            return View(service);
        }
2
Contributors
14
Replies
46
Views
4 Years
Discussion Span
Last Post by ryan311
Featured Replies
  • I got it already. It's regular expression problem. waaaaaaaaa it's working properly now. Thank you mitch for helping :) Read More

0

I always getting this error in my dropdownlistfor:

How does this function works:

new { @onchange = "GetSiteid(this.value);" }

Does it get the valve?

@Html.DropDownListFor(model => model.SiteId, new SelectList(ViewBag.ListSiteClass, "SiteId", "SiteName"), "", new { @onchange = "GetSiteid(this.value);" })

It should look like this:

@Html.DropDownListFor(model => model.SiteId, (IEnumerable<SelectListItem>)ViewBag.ListSiteClass, "SiteId", "SiteName",  "Select  SiteId", new { ID = "SiteId" })
@Html.ValidationMessageFor(model => model.SiteId)

I don't don't how your code works, but it's the SiteID that is null so you need to do this:

public int ID { get; set; }

public int SiteId 
{ 
    get { return ID; } 
    set { ID = value; } 
}

Edited by LastMitch: grammer

0

Hi mitch,

Does it get the valve?

@Html.DropDownListFor(model => model.SiteId, new SelectList(ViewBag.ListSiteClass, "SiteId", "SiteName"), "", new { @onchange = "GetSiteid(this.value);" })

Yes.

It should look like this:

@Html.DropDownListFor(model => model.SiteId, (IEnumerable<SelectListItem>)ViewBag.ListSiteClass, "SiteId", "SiteName",  "Select  SiteId", new { ID = "SiteId" })

-I got error on this line. "it takes 6 arguments".

In my Model:

public int SiteId { get; set; }
0

How does this function works:

new { @onchange = "GetSiteid(this.value);" }



<script type="text/javascript">
    function GetSiteid(_siteid) {
        var procemessage = "<option value=''> Please wait...</option>";
        $('#BuildingId').html(procemessage).show();
        var url = "/Service/GetSiteId/";
        $.ajax({
            url: url,
            data: { siteid: _siteid },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value=''></option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $('#BuildingId').html(markup);
            }
        });

    }
</script>
0

I got error on this line. "it takes 6 arguments".

The error means (you can fixed it if you want to keep your code) this:

http://stackoverflow.com/questions/5677056/asp-net-mvc3-receiving-the-no-overload-for-method-beginform-takes-6-argumen

How does this function works:

I kinda had a feeling that you are using some sort of javascript.

This code is not part of ASP.net:

@onchange = "GetSiteid(this.value)"

it's javscript.

That's why I ask you does it work. You said it does.

You can used this:

http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx

It will save you alot of time. If you used the code from the link then you don't have to do much. But if you decide to keep your current code then you need to fixed the 6 arguments and then do the javacript.

Edited by LastMitch: grammer

0

Hi mitch thank you for your reply but still i got this error

Value cannot be null.
Parameter name: items.

@Html.DropDownList("SiteId", new SelectList(ViewBag.ListSiteClass, "SiteId", "SiteName", Model.SiteId), "Select Site")

I already change everything it works perfectly. but still i got this error.

0

Hi mitch,

So weird when i erase this code

if (Modelstate.Isvalid)
{
}

Everything execute Ok.

But is there anyway i can validate a textfield.

because i have AssetTotal text field in View which is i need to validate.

I want this AssetTotal value must greater than to 0.

If assettotal is zero then i will execute this code

int sid = ms.SiteId;
ServiceDAO.Insert(sid);

else will return to view.

Regards,
Ryan

0

Hi mitch thank you for your reply but still i got this error

Base on the code you provided it's the SiteId.

@Html.DropDownList("SiteId", new SelectList(ViewBag.ListSiteClass, "SiteId", "SiteName", Model.SiteId), "Select Site")

I hope you know there is more code involved regarding about the error you are having.

You can take a look at these example which has similiar issue as yours:

http://stackoverflow.com/questions/3395527/error-in-asp-net-mvc-selectlist-value-cannot-be-null-parameter-name-items

http://stackoverflow.com/questions/3244336/using-linq-to-find-item-in-a-list-but-get-value-cannot-be-null-parameter-name

So weird when i erase this code

Can you used this code instead of the one you have:

http://randomtype.ca/blog/how-to-test-modelstate-isvalid-in-asp-net-mvc/

public ActionResult Create(ServiceDTO ms, FormCollection form){
if (ModelState.IsValid){
int sid = ms.SiteId;
ServiceDAO.Insert(sid);
}
else{
return View();
}
}

You mixing code with other languages. I'm not familiar with Java.

The link I provided above show you how to test ModelState.IsValid with ASP.net only.

0

I use C# MVC 3 RAZOR VIEW ENGINE.

I can tell you I might not know alot stuff in ASP.net.

This code is not View Engine:

public ActionResult Create(ServiceDTO ms, FormCollection form){
if (ModelState.IsValid){
int sid = ms.SiteId;
ServiceDAO.Insert(sid);
}
else{
return View();
}
}

You can take a look at this (it's in C#):

http://www.waynehaffenden.com/Blog/ASPNET-MVC-3-Razor-CSharp-View-Engine

Did you create and define this:

SiteId or Model.SiteId

Cause 1 of those are null and is causing the argument.

From this:

@Html.DropDownList("SiteId", new SelectList(ViewBag.ListSiteClass, "SiteId", "SiteName", Model.SiteId), "Select Site")

to this:

@Html.DropDownListFor(model => Model.SiteId, new SelectList((IEnumerable)ViewBag["ListSiteClass"], "SiteId", "SiteName"))

What error did you get?

On here:

public int SiteId { get; set; }

it should be this:

public int SiteId
{
get { return ID; }
set { ID = value; }
}

In the controller:

ViewBag["ListSiteClass"] = SiteId.ListSiteClass().ToList();
0

This is my code in controller

List<SiteDTO> LSSiteNameList = SiteDAO.GetSiteNameList(User.Identity.Name);
            ViewBag.ListSiteClass = LSSiteNameList;

in SiteDAO Model

public static List<SiteDTO> GetSiteNameList(string username)
        {
            using (SqlConnection objcon = new SqlConnection(connectionString))
            {
                objcon.Open();

                using (SqlCommand cmd = new SqlCommand("sp_Site", objcon) { CommandType = CommandType.StoredProcedure })
                {
                    cmd.Parameters.AddWithValue("Action", "GetSiteList");
                    cmd.Parameters.AddWithValue("siteid", "");
                    cmd.Parameters.AddWithValue("sitename", "");
                    cmd.Parameters.AddWithValue("sitemanagerid", "");
                    cmd.Parameters.AddWithValue("serviceperson", username);

                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        DataTable table = new DataTable();
                        table.Load(reader);
                        List<SiteDTO> LSSiteList = new List<SiteDTO>();

                        foreach (DataRow row in table.Rows)
                        {
                            SiteDTO ms = new SiteDTO();
                            ms.Siteid = Convert.ToInt32(row["SiteID"].ToString());
                            ms.Sitename = row["SiteName"].ToString();
                            LSSiteList.Add(ms);
                        }
                        return LSSiteList;
                    }
                }
            }
        }
0

What error did you get?

cmd.Parameters.AddWithValue("siteid", "");
cmd.Parameters.AddWithValue("sitename", "");

Your siteid is lowercase and your row["SiteID"] is different.

ms.Siteid = Convert.ToInt32(row["SiteID"].ToString());
ms.Sitename = row["SiteName"].ToString();

It should look the same:

SiteID = row["SiteID"] 
SiteName = row["SiteName"]

It has to be the same name in the database for it to fetch the data from the database.

Edited by LastMitch: grammer

0

Value cannot be null. Parameter name: items

I don't know what else to tell you. It seen you are using jQuery with ASP.net so it's bit hard to pin point the issue of which is causing this. Have you post this on the ASP.net forum?

I mention this alot of time but it seem you don't understand what I am talking about.

@Html.DropDownListFor(model => Model.SiteId, new SelectList((IEnumerable)ViewBag["ListSiteClass"], "SiteId", "SiteName"))

Try this it will see which is null:

GetSiteNameList(List<SiteDTO> GetSiteNameList)
{
//null-check
if(SiteId == null)
        throw new NullReferenceException("Cannot be null!!!");
// Select 
select new
{
int sid = ms.SiteId;
ServiceDAO.Insert(sid);
    }
}
1

I got it already. It's regular expression problem. waaaaaaaaa it's working properly now. Thank you mitch for helping :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.