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 7762439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:24:11+00:00 2026-06-01T14:24:11+00:00

I can’t get the ObjectId for a model to return back with a value

  • 0

I can’t get the ObjectId for a model to return back with a value using the MongoDB csharp official library. When I submit the form that posts back to the controller, the PageID always comes back {000000000000000000000000} in the model. The HTML rendered has the valid id, that one field never comes back properly from the form post.

The html renders: <input id="PageID" name="PageID" type="hidden" value="4f83b5ccd79f660b881887a3" />

This is what I have.

Model:

public class PageModel
    {
        public PageModel()
        {
            CanEdit = false;
            CancelRequest = false;
        }

        [BsonId]
        public ObjectId PageID { get; set; }

        [Display(Name = "Page URL")]
        public string PageURL { get; set; }

        [AllowHtml]
        [Display(Name = "Content")]
        public string Value { get; set; }

        [Display(Name = "Last Modified")]
        public DateTime LastModified { get; set; }

        [BsonIgnore]
        public bool CancelRequest { get; set; }
        [BsonIgnore]
        public bool CanEdit { get; set; }

    }

Controller Post:

[HttpPost]
        public ActionResult Edit(PageModel model)
        {
            // check to see if the user canceled
            if (model.CancelRequest)
            {
                return Redirect(model.PageURL);
            }

            // check for a script tag to prevent
            if (!model.Value.Contains("<script"))
            {
                // save to the database
                model.LastModified = DateTime.Now;
                collection.Save(model);

                // return to the page
                return Redirect(model.PageURL);
            }
            else
            {
                ModelState.AddModelError("Value", "Disclosures discovered a script in the html you submitted, please remove the script before saving.");
            }

            return View(model);
        }

View:

@model LeulyHome.Models.PageModel

@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{ 
    <fieldset>
        <legend>Edit Page</legend>
        <div class="control-group">
            @Html.LabelFor(m => m.PageURL, new { @class = "control-label" })
            <div class="controls">
                @Html.TextBoxFor(m => m.PageURL, new { @class = "input-xlarge leu-required span9" })
                @Html.ValidationMessageFor(m => m.PageURL, null, new { @class = "help-block" })
            </div>
        </div>
        <div class="control-group">
            @Html.LabelFor(m => m.Value, new { @class = "control-label" })
            <div class="controls">
                @Html.TextAreaFor(m => m.Value)
                @Html.ValidationMessageFor(m => m.Value, null, new { @class = "help-block" })
            </div>
        </div>
        <div class="control-group">
            @Html.LabelFor(m => m.LastModified, new { @class = "control-label" })
            <div class="controls">
                @Html.DisplayFor(m => m.LastModified)
                @Html.HiddenFor(m => m.LastModified)
            </div>
        </div>

        @Html.HiddenFor(m => m.PageID)
        @Html.HiddenFor(m => m.CancelRequest)

        <div class="form-actions">
            <button type="submit" class="btn btn-primary">Save Page</button>
            <button type="button" class="btn btn-danger" id="cancelBtn">Cancel Changes</button>
        </div>
    </fieldset>
}

@section Footer {
    <script type="text/javascript" src="@Url.Content("~/Content/ckeditor/ckeditor.js")"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            // adjust the editor's toolbar
            CKEDITOR.replace('Value', {
                toolbar: [["Bold", "Italic", "Underline", "-", "NumberedList", "BulletedList", "-", "Link", "Unlink"],
                  ["JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock"],
                  ["Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt"], ["Source", "About"]]
            });

            $("#cancelBtn").click(function () {
                $("#CancelRequest").val("True");
                $("#updateBtn").click();
            });
        });
    </script>
}
  • 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-06-01T14:24:13+00:00Added an answer on June 1, 2026 at 2:24 pm

    It looks like you are sending a string value for PageID which you are expecting an object of type ObjectId.

    The model binding isn’t going to know how to take this string and turn it into an ObjectId. If you take a look at the ObjectId class in the MongoDriver you will see that it is pretty hairy.

    We use mongodb quite a bit and for our id’s we simply use strings which provide a lot of flexibility. I am not sure the use case for which you need the ObjectId class as your document Id but you may not need it.

    So from here it seems like you have two options.

    1. Change your document id’s to strings or something else
    2. Write a custom model binder for the ObjectId class

    Hope that helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't seem to get the Back Button to appear in a UINavigationController flow. I
Can someone recommend an up to date library for data Sanitization in PHP ?
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anyone give me an example of how to return the following json simply
Can PHP PDO extension bind nested objects automatically ? I mean using foreign key
Can some one Guide me to work with these things... What is Model popup
Can we close all known/unknown connections to database with the code? I'm using Access
Can I convert a pdf to pcl file with ghostscript? I'm using Ghostscript 9.01
Can i get the source code for a WAMP stack installer somewhere? Any help
Can I open my custom developed form [instead of native one] when user opens

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.