Passing parameter across controllers

余生颓废 提交于 2019-12-11 20:57:54

问题


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>

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();

}

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

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>

I have tried

@Html.HiddenFor(ViewBag.AssetID)

however I cant get it to compile error:

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.

Any help would be appreciated.


回答1:


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

Controller

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

View

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


来源:https://stackoverflow.com/questions/25986420/passing-parameter-across-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!