在控制器之间传递参数 [英] Passing parameter across controllers

查看:111
本文介绍了在控制器之间传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是保存在一个与项目在当前局部视图相关的不同控制器的记录。我有一个细节查看此使用以下code不同的表会显示相关记录列表:

My goal is to save a record in different controller that is associated with a item in a current detail view. I have a detail view this displays a list of associated records from a different table using the following code:

<table class="table">
    <tr>
        <th>
            Date
        </th>
        <th>
            Notes
        </th>
        <th>
            Contractor
        </th>
    </tr>

    @foreach (var item in Model.ServiceHistories)
    {
        <tr>
            <td width="200px">
                @Html.DisplayFor(modelItem => item.Date)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Notes)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ContractorID)
            </td>
        </tr>
    }

    @Html.ActionLink("Create", "Create", "ServiceHistories", new { id = Model.AssetID }, null)

</table>

在底部,我添加了一个动作链接到一个动作在不同的控制器,通过传递由assetid该资产创造的资产了新的服务历史记录。这是创建(POST和GET)的服务历史的行动:

At the bottom I have added an Action link to a action in a different controller to create a new Service History record for that asset by passing in the AssetID for that asset. This is the Create (POST and GET) action for Service History:

// GET: ServiceHistories/Create
public ActionResult Create(int? id)
{
    ViewBag.AssetID = id;
    return View();
}

// POST: ServiceHistories/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ServiceID,AssetID,Date,ContractorID,Notes")] ServiceHistory serviceHistory)
{
    if (ModelState.IsValid)
    {
        db.ServiceHistories.Add(serviceHistory);
        db.SaveChanges();
        return RedirectToAction("Details", "Assets", new { id = serviceHistory.AssetID });
    }

    ViewBag.AssetID = new SelectList(db.Assets, "AssetID", "Description", serviceHistory.AssetID);
    return View();

}

我已经加入(INT标识)作为参数,以创建行动,并将其分配给whic被传递到视图ok了,我可以在页面上显示它ViewBg.AssetID。我的问题是

I have added (int Id) as a parameter to the Create action and assigned it to ViewBg.AssetID whic is is passed into the view ok as I can display it on the page. My problem is

我的拳头的问题是我怎么使用这个值来代替低于code。即我想隐藏由assetid领域和使用参数ViewBag.AssetID来代替。

My fist question is how do I use this value to replace the code below. I.e. I would like to hide the AssetID field and use the parameter ViewBag.AssetID instead.

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

我曾尝试

@Html.HiddenFor(ViewBag.AssetID)

但我不能得到它来编译错误:

however I cant get it to compile error:

编译器错误信息:CS1973:'System.Web.Mvc.HtmlHelper'中有一个名为没有适用的方法HiddenFor,但似乎有这个名字的扩展方法。扩展方法不能进行动态调度。考虑强制转换动态参数或调用没有扩展方法的语法扩展方法。

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'HiddenFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

我已经阅读文章和教程,来接近这个的负载,但我似乎可以破解什么我做错了。

I have read a load of posts and tutorials that come close to this but I can seem to crack what I am doing wrong.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

不知道为什么你会因为你的模型分配这 ViewBag ServiceHistory 有一个属性由assetid

Not sure why you would assign this to ViewBag since your model ServiceHistory has a property AssetID.

控制器

public ActionResult Create(int? id)
{
  ServiceHistory model = new ServiceHistory();
  model.AssetID = id;
  return View(model);
}

查看

@model YourAssembly.ServiceHistory
....
@Html.HiddenFor(m => m.AssetID)

这篇关于在控制器之间传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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