Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对编辑和新建操作使用相同的局部视图 .NET 4.51,MVC 5_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 对编辑和新建操作使用相同的局部视图 .NET 4.51,MVC 5

C# 对编辑和新建操作使用相同的局部视图 .NET 4.51,MVC 5,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个视图Edit.cshtml,如下所示: @model EnergyMission.Country.Dtos.GetCountryOutput @{ ViewBag.Title = "Edit Country"; Layout = "~/Views/Shared/_Layout.cshtml"; } @Html.Partial("_AddEditPartial", new ViewDataDictionary { { "ActionName",

我有一个视图Edit.cshtml,如下所示:

@model EnergyMission.Country.Dtos.GetCountryOutput

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

@Html.Partial("_AddEditPartial", new ViewDataDictionary { { "ActionName", "Edit" } })
@model EnergyMission.Country.Dtos.GetNewCountryOutput

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

@Html.Partial("_AddEditPartial", new ViewDataDictionary { { "ActionName", "Create" } })
@model  EnergyMission.Country.Dtos.GetCountryOutput

<h3>@ViewData["ActionName"]</h3>

@using (Html.BeginForm(@ViewData["ActionName"], "Countries"))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>CountryTableModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.AbbreviationCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AbbreviationCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.AbbreviationCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InternationalCountryDialCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InternationalCountryDialCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InternationalCountryDialCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
}
然后新建.cshtml,如下所示:

@model EnergyMission.Country.Dtos.GetCountryOutput

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

@Html.Partial("_AddEditPartial", new ViewDataDictionary { { "ActionName", "Edit" } })
@model EnergyMission.Country.Dtos.GetNewCountryOutput

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

@Html.Partial("_AddEditPartial", new ViewDataDictionary { { "ActionName", "Create" } })
@model  EnergyMission.Country.Dtos.GetCountryOutput

<h3>@ViewData["ActionName"]</h3>

@using (Html.BeginForm(@ViewData["ActionName"], "Countries"))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>CountryTableModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.AbbreviationCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.AbbreviationCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.AbbreviationCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.InternationalCountryDialCode, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.InternationalCountryDialCode, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.InternationalCountryDialCode, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>
}
从上面可以看出:

  • 每个都有一个不同的模型,即GetCountryOutputGetNewCountryOutput。两者具有相同的属性。目前,由于其他原因,它们不能成为同一类
  • 我传递动作名称,以便在局部视图中的Html.BeginForm中使用它
  • 我仍然需要弄清楚如何将模型类型传递给局部视图
  • \u addeditparticle.cshtml如下所示:

    @model EnergyMission.Country.Dtos.GetCountryOutput
    
    @{
        ViewBag.Title = "Edit Country";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }             
    
    @Html.Partial("_AddEditPartial", new ViewDataDictionary { { "ActionName", "Edit" } })
    
    @model EnergyMission.Country.Dtos.GetNewCountryOutput
    
    @{
        ViewBag.Title = "New Country";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    @Html.Partial("_AddEditPartial", new ViewDataDictionary { { "ActionName", "Create" } })
    
    @model  EnergyMission.Country.Dtos.GetCountryOutput
    
    <h3>@ViewData["ActionName"]</h3>
    
    @using (Html.BeginForm(@ViewData["ActionName"], "Countries"))
    {
        @Html.AntiForgeryToken()
    
        <div class="form-horizontal">
            <h4>CountryTableModel</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.AbbreviationCode, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.AbbreviationCode, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.AbbreviationCode, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.InternationalCountryDialCode, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.InternationalCountryDialCode, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.InternationalCountryDialCode, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
    }
    
    然后在_AddEdit.cshtml中

    _

  • 为什么以下内容无效:
  • _


    因为这给了我一个编译时错误。

    那么答案是3。是html.beginform需要一个字符串作为它的第一个参数,并且当您使用ViewData时,它是一个

    Dictionary<string, object> 
    

    1。您可以尝试从基类派生两个dto类,并将该类设置为“添加”和“编辑”视图的模型类型创建单个视图,然后使用控制器指向单个视图,并使用存储库在保存方法中相应地插入/更新。但是,请注意,在下面的示例中,您需要确保模型是相同的-基于消除重复的原则。如果不可能考虑在你的视图中使用动态模型或者使用VIEW模型,你可以相应地映射属性(无论如何,我不想使答案变得过于复杂,因为我希望这提供了你需要的解决方案——在这里和那里进行一些重构)。 控制器和存储库的简化示例(使用viewmodel)

    存储库

    public class SqlProductRepository : IProductRepository
    {
         private MyDataContext db;
         private Table<Product> _productsTable; 
    
        public IQueryable<Product> Products
        {
            get { return _productsTable; }
        }
        public void Save(Product product)
        {
            if (product.ProductID == 0)
            {
                _productsTable.InsertOnSubmit(product);
            }
            else if (_productsTable.GetOriginalEntityState(product) == null)
            {
                _productsTable.Attach(product);
                _productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
            }
            _productsTable.Context.SubmitChanges();
        }
    
    公共类SqlProductRepository:IPProductRepository { 私有MyDataContext数据库; 私有表_productsTable; 公共卫生产品 { 获取{return\u productsTable;} } 公共作废保存(产品) { if(product.ProductID==0) { _产品稳定。InsertOnSubmit(产品); } else if(_productsTable.GetOriginalEntityState(product)==null) { _产品稳定。附加(产品); _productsTable.Context.Refresh(RefreshMode.KeepCurrentValues,product); } _productsTable.Context.SubmitChanges(); }
    }

    谢谢您的修复。我意识到我可以从同一个基类派生,但出于其他原因(即我无法控制该类的层次结构),我需要想出一个替代解决方案。我明白了,然后,一种可行的方法是使用您自己的ViewModel,然后通过使用映射器实用程序(如automapper或ValueInjectorTanks)简化将数据移入和移出DTO的过程。我用它作为我最终实现的基础。很高兴它有用;)