Pages

Showing posts with label stupid programmer tricks. Show all posts
Showing posts with label stupid programmer tricks. Show all posts

Wednesday, March 21, 2018

MVC: A public action method ... was not found on controller

You have a page with something embedded, a partial view, a generated image, or something.  And it's odd, but the first time you load the page, everything works, but every time you reload (like someone clicks a button on the page), MVC claims it can't find the action.

InnerException:
ErrorCode=-2147467259
HResult=-2147467259
Message=A public action method ... was not found on controller '...Controller'.
Source=System.Web.Mvc
WebEventCode=0

You check and the action is right there!.  In my case it looked like this.

//Get: ProductPartial Blocks
[HttpGet]
[AllowAnonymous]
public ActionResult ProductBlockPartial(int? ID)
{
if (ID.HasValue)
{
Product model = db.Products.Find(ID);
return PartialView(model);
}
else
{ return null; }
}

The answer, my friend, is that pesky [HttpGet].  That tells the action to only respond to get requests, and when you pushed the button to post data back to the page, it became a post.  And the post flows down from the main page to the partial view as well.
Just remove the [HttpGet] from the action and you're good as gold!

...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Friday, January 18, 2013

DBNulls: the Bane of my Existence

So yes, a DataGridViewCell.value is an object.  But it seems there should be a way to tell it "hey, in this case, when I ask for a value and you have a null, just hand me an empty string".

So I wrote this (for use until I figure out how to default a null cell to string.empty).

        /// <summary>
        /// Converts an object value to a string.
        /// (usually from a DataGridViewCell.Value;
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public string Val2Str(object value)
        {
            if (value == DBNull.Value)
            {
                return string.Empty;
            }
            return value.ToString();
        }

        /// <summary>
        /// Converts a DataGridViewCell value to a string.
        /// </summary>
        /// <param name="Cell">The cell.</param>
        /// <returns></returns>
        public string Val2Str(DataGridViewCell Cell)
        {
            return Val2Str(Cell.Value);
        }

These overloaded routines can be passed either a DataGridViewCell itself, or just the Value property.  Kinda like this:

string customer = Val2Str(dataGridView1.CurrentRow.Cells["Customer"]);
string customer = Val2Str(dataGridView1.CurrentRow.Cells["Customer"].Value);

...

Bryan Valencia is a contributing editor and founder of Visual Studio Journey.  He owns and operates Software Services, a web design and hosting company in Manteca, California.

Share This!

Contact Us

Name

Email *

Message *