2

Hi there.

For more than a day now I have been unable to update my entity models in my MVC project with values from the post. I have read like 30 related issues here on SO and still I have no idea what is even wrong. I made a small project without Ajax and other disturbances, and now I can watch it fail reliably. I was hoping to run this by you, it is very simple, so perhaps someone more experienced can easily identify my misstake. This is what I came up with:

Models and data:

    public class StoryDB : DbContext
{
    public DbSet<Story> Stories { get; set; }

}

public class Story
{
    [Key]
    public int StoryID { get; set; }
    public string zestory { get; set; }
}

public class StoryViewModel
{
    public Story stry;
}

Controller:

    //
    // GET: /Story/Create
    [HttpGet]
    public ActionResult Create()
    {
        StoryViewModel svm = new StoryViewModel();
        svm.stry = new Story();

        return View(svm);
    }

    //
    // POST: /Story/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(StoryViewModel svm)
    {
        if (ModelState.IsValid)
        {
            db.Stories.Add(svm.stry);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(svm);
    }

View:

@model MvcApplication7.Models.StoryViewModel

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Story</legend>
    @Html.HiddenFor(model => model.stry.StoryID)
    <div class="editor-label">
        @Html.LabelFor(model => model.stry.zestory)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.stry.zestory)
        @Html.ValidationMessageFor(model => model.stry.zestory)
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

This is the HTML it produces:

<form action="/Story/Create" method="post"><input name="__RequestVerificationToken" type="hidden" value="v0wlNDXbhykfe6IKOep6WMSZVteTDNPdgUVkQ8VcW4RW2oo-ufJSdVkN70Y0OHI4if56HrZiGLaGVy9UzlsoTkKXm963ZwRfJsuBZSVs9uQ1" />    <fieldset>
    <legend>Story</legend>
    <input data-val="true" data-val-number="The field StoryID must be a number." data-val-required="The StoryID field is required." id="stry_StoryID" name="stry.StoryID" type="hidden" value="0" />
    <div class="editor-label">
        <label for="stry_zestory">zestory</label>
    </div>
    <div class="editor-field">
        <input class="text-box single-line" id="stry_zestory" name="stry.zestory" type="text" value="" />
        <span class="field-validation-valid" data-valmsg-for="stry.zestory" data-valmsg-replace="true"></span>
    </div>
    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
</form>

And what happens is that there is no databinding I can notice, and it crashes on trying to save with an error due to the incomming viewmodel's Story stry being null.

I have also tried to create new viewmodel and belonging story and filling them with TryUpdateModel() with the result that empty entries were made into the database.

Possibly relevant:

The Crash

Viewmodel referencetype is null

Modelstate

4

1 回答 1

2

尝试将字段更改为属性:

public class StoryViewModel
{
    public Story stry { get; set; }
}
于 2013-08-13T13:43:37.680 回答