Skip to main content
 首页 » 编程设计

c#之MongoDB:MVC 未在发布时序列化 BsonId

2024年02月23日16txw1958

我无法使用 MongoDB csharp 官方库获取模型的 ObjectId 以返回值。当我提交回传给 Controller 的表单时,PageID 总是在模型中返回 {0000000000000000000000000}。呈现的 HTML 具有有效 ID,该字段永远不会从表单发布中正确返回。

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

这就是我的。

型号:

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 职位:

[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); 
        } 

查看:

@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> 
} 

请您参考如下方法:

看起来您正在为 PageID 发送一个字符串值,您希望它是一个 ObjectId 类型的对象。

模型绑定(bind)不知道如何获取此字符串并将其转换为 ObjectId。如果您查看 MongoDriver 中的 ObjectId 类,您会发现它非常复杂。

我们经常使用 mongodb,对于我们的 id,我们只使用提供了很大灵 active 的字符串。我不确定您需要 ObjectId 类作为文档 ID 的用例,但您可能不需要它。

从这里看来您有两个选择。

  1. 将您的文档 ID 更改为字符串或其他内容
  2. 为 ObjectId 类编写自定义模型绑定(bind)器

希望对您有所帮助:)