Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:59:28+00:00 2026-05-28T14:59:28+00:00

I’m having some trouble getting my ViewModel to return a non-null object in the

  • 0

I’m having some trouble getting my ViewModel to return a non-null object in the base model property in my controller’s Create action postback. I currently have another page that is doing the exact same operations on another model that has almost the same properties and that form is working perfectly, so I feel like I’m missing something basic, though I can’t place what is wrong.

Here’s my ViewModel with my Base class:

public class SystemFailureActionViewModel
{
    /// <summary>
    /// View model class for adding and modifying SystemFailureActions
    /// </summary>
    public SystemFailureAction action { get; set; }

    //properties
    public int TypeID { get; set; }
    public string TypeDescription { get; set; }
    public bool Assigned { get; set; } 

    public SystemFailureActionViewModel() { }

    public SystemFailureActionViewModel(SystemFailureAction action)
    {
        this.action = action;
    }

    //Collections for views
    public IEnumerable<SelectListItem> EditSystemFailiureTypesList { get { return ModelListProvider.FilteredSystemFailureTypeList; } }
    public IEnumerable<SelectListItem> DetailsSystemFailiureTypesList { get { return ModelListProvider.FullSystemFailureTypeList; } }
}

[MetadataType(typeof(SystemFailureActionMetadata))]
public partial class SystemFailureAction
{
    private class SystemFailureActionMetadata
    {
        /// <summary>
        /// ID for this failure action
        /// </summary>
        public int ID { get; set; }

        /// <summary>
        /// Action Description
        /// </summary>
        [Required]
        [StringLength(200)]
        [DisplayName("Action Description")]
        public string Description { get; set; }
    }
}

Here’s my Controller Add and Add Postback methods:

public ActionResult Add()
    {
        SystemFailureAction action = new SystemFailureAction();
        action.Description = "";
        populateSystemFailureActionData(action);
        return PartialView("Form", new SystemFailureActionViewModel(action));
    }


    [HttpPost]
    public ActionResult Add(SystemFailureActionViewModel viewModel, string[] selectedTypes, FormCollection collection)
    {
        SystemFailureAction newAction = viewModel.action;

        if (!ModelState.IsValid)
        {
            populateSystemFailureActionData(newAction);
            return PartialView("Form", new SystemFailureActionViewModel(newAction));
        }

        try
        {
            //Insert the new failure action type 
            context.SystemFailureActions.InsertOnSubmit(newAction);
            context.SubmitChanges();

            //Insert the failure type mappings
            updateSystemFailureAssociationData(newAction, selectedTypes);
            context.SubmitChanges();

            //Return the new data
            populateSystemFailureActionData(newAction);
            return PartialView("Done", new SystemFailureActionViewModel(newAction));
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.Message);
            populateSystemFailureActionData(newAction);
            return PartialView("Form", new SystemFailureActionViewModel(newAction));
        }           
    }      

And finally here is my form, it is being loaded into a Jquery dialog and the postback is being done via Ajax.

  @using (Ajax.BeginForm(new AjaxOptions
{
    HttpMethod = "Post",
    UpdateTargetId = "formDialog",
    InsertionMode = InsertionMode.Replace,
    OnSuccess = "onDialogDone()"
}))
{
    @Html.ValidationSummary(true)
    <div>
        <div class="editor-label">
            @Html.LabelFor(model => model.action.Description)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.action.Description)
        </div>
    </div>
    <div class="categoryFieldSet">
        <fieldset>
            <legend>Mechanical System Failure Categories</legend>
            <div class="editor-field">
                <table>
                    <tr>
                        @{
                            int count = 0;
                            List<ManageMAT.ViewModels.SystemFailureActionViewModel> types = ViewBag.Types;

                            foreach (var type in types)
                            {
                                if (count++ % 3 == 0)
                                {
                                    @: </tr> <tr>
                                }
                                @: <td>
                                    <input type="checkbox"
                                           id="selectedTypes + @type.TypeID"
                                           name="selectedTypes"
                                           value="@type.TypeID"
                                           @(Html.Raw(type.Assigned ? "checked=\"checked\"" : "")) />
                                    <label for="selectedTypes + @type.TypeID">@type.TypeDescription</label>
                                @:</td>
                            }
                        @:</tr>
                        }
                </table>
            </div>
        </fieldset>
    </div>
}

If you’re wondering what the ViewBag.Types logic is in the form it is related to this question I asked earlier.

Edit:

I checked the ModelState error and the exception I’m getting is

"The parameter conversion from type 'System.String' to type Models.SystemFailureAction' failed because no type converter can convert between these types."

I also removed the logic that adds the Failure types and I’m still receiving the same issue. So it appears the problem is coming from mapping the Viewmodel.action.Description to the action.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:59:29+00:00Added an answer on May 28, 2026 at 2:59 pm

    The problem is the following property on your SystemFailureActionViewModel:

    public SystemFailureAction action { get; set; }
    

    In ASP.NET MVC action and controller are kinda reserved words. They are part of every route. And if you have a property called this way it conflicts with the string value which is pointing to the action name and which obviously cannot be bound to a complex SystemFailureAction type.

    So to fix the problem simply rename this property on your view model:

    public SystemFailureAction FailureAction { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
i got an object with contents of html markup in it, for example: string
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.