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>
}
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.
Hope that helps 🙂