ASP.Net MVC框架的实体模型提交,然后打开编辑页面新模式 [英] ASP.Net MVC entity framework submit model, then open new model in edit page

查看:103
本文介绍了ASP.Net MVC框架的实体模型提交,然后打开编辑页面新模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提交模型的形式。控制器将保存模型到数据库,然后创建一个新的模型,并返回与新模式的EditCreate视图。

我遇到的问题是,不被显示的新模式。相反,刚张贴的模型所示。我跟着它通过调试视图,而看到的值是新的,但是当页面显示,它显示了所有刚刚提交的旧值。

控制器

  [HttpPost]
公众的ActionResult addProduct命令(MyEntityModel基于myModel)
{
    // 保存
    如果(myModel.ID!= 0){
        更新(基于myModel);
    }其他{
        添加(基于myModel);
    }    ModelState.Clear(); //这个固定    //显示的新模式页
    MyEntityModel = newModel,并向新MyEntityModel();
    newModel.ID = 0;    //编辑/创建页
    RET的ActionResult = EditCreateRow(newModel,并向);    返回RET;
}

示范

 公共部分类STM_AuditData
{
    公众诠释ID {搞定;组; }
    公众可空<&System.DateTime的GT; ServiceDate {搞定;组; }
    公共字符串产品{搞定;组; }
}

查看

  @ Html.LabelFor(型号=> model.ServiceDate)
@ Html.EditorFor(型号=> model.ServiceDate)
@ Html.LabelFor(型号=> model.Product)
@ Html.EditorFor(型号=> model.Product)
@ Html.HiddenFor(型号=> model.AuditID)

我能做些什么,以确保正确的模特表演?谢谢你。

这里的code为EditCreateRow

 公众的ActionResult EditCreateRow(MyEntityModel行)
{
    MyEntityModel.IntsToBools(REF行);    EditCreate_SetViewBagItems(行);    返回视图(〜/查看/ MyEntityModel / EditCreate.cshtml行);
}


解决方案

原因是这样的话,是的HtmlHelper如何工作的。当设置在HTML元素的值,它有几种选择来获得的值,使用IValueProvider的实现。您可以检查在<一个默认列表href=\"https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/ValueProviderFactories.cs\"相对=nofollow> aspnetwebstack 。相比更好的MVC框架在那里,ASP.NET MVC是反直觉的。

如果您添加

  ModelState.Clear()

在返回前,我想你可能将它解决了。

I've got a form that submits a model. The controller saves that model to the database, then creates a new model and returns an EditCreate view with that new model.

The problem I'm having is that the new model isn't being displayed. Instead the model that was just posted is shown. I follow it through the debugger to the view and see the values are new, but when the page displays, it shows all the old values that were just submitted.

Controller

[HttpPost]
public ActionResult AddProduct(MyEntityModel myModel)
{
    // Save
    if (myModel.ID != 0) {
        Update(myModel);
    } else {
        Add(myModel);
    }

    ModelState.Clear(); // This fixed it

    // Show page for new model
    MyEntityModel newModel = new MyEntityModel();
    newModel.ID = 0;

    // Edit/Create page
    ActionResult ret = EditCreateRow(newModel);

    return ret;
}

Model

public partial class STM_AuditData
{
    public int ID { get; set; }
    public Nullable<System.DateTime> ServiceDate { get; set; }
    public string Product { get; set; }
}

View

@Html.LabelFor(model => model.ServiceDate)
@Html.EditorFor(model => model.ServiceDate)
@Html.LabelFor(model => model.Product)
@Html.EditorFor(model => model.Product)
@Html.HiddenFor(model => model.AuditID)

What can I do to ensure the correct model shows? Thanks.

Here's the code for EditCreateRow

public ActionResult EditCreateRow(MyEntityModel row)
{
    MyEntityModel.IntsToBools(ref row);

    EditCreate_SetViewBagItems(row);

    return View("~/Views/MyEntityModel/EditCreate.cshtml", row);
}

解决方案

The reason this is the case, is how the HtmlHelper works. When setting the value in the HTML-element, it has several options to get the value, using implementations of IValueProvider. You can check the default list at the aspnetwebstack. Compared to the better MVC frameworks out there, ASP.NET MVC is counter-intuitive.

If you add

ModelState.Clear()

before returning, I think you might have it solved.

这篇关于ASP.Net MVC框架的实体模型提交,然后打开编辑页面新模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆