繁体   English   中英

将actionLink提交到表单mvc4

[英]Submitting an actionLink to a form mvc4

我们有一个动作链接列表

部分视图

@foreach (var item in Model.Regions) {
    <tr>

    <td>
        @Html.DisplayFor(modelItem => item.RegionName)
    </td>

    <td>
        <input type="submit" value="Select" />
    </td>

     @Html.HiddenFor(modelItem => Model.Id)
</tr>
}
</table>

我认为这不是执行此操作的正确方法,但是如果您可以指出正确的方向,我们将不胜感激。 我想将此数据提交到现有表单中

区域视图

@using (Html.BeginForm()){
<fieldset>
    @Html.Partial("_RegionsPartial");
<legend>Create new region</legend>
<ol>
    <li>@Html.LabelFor(m => m.RegionName)</li>
    <li>@Html.EditorFor(m => m.RegionName)</li>
</ol>
    <input type="submit" value="Next" />
    @Html.HiddenFor(model => model.RegionId)
</fieldset>
}

因此,您可以提交一个新的或提交一个现有的。 我不确定如何将现有ID放入我的模型中。 这是控制器:

    public ActionResult Region()
    {
        var model = new WizardModel();
        var getRegions = _facade.FetchRegion();
        model.Regions = getRegions;
        return View(model);
    }


    [HttpPost]        
    public ActionResult Region(WizardModel model)
    {
        if (model.RegionName != null)
        {
            var newRegion = _facade.CreateRegion(model.RegionName);
            model.RegionId = newRegion.Id;
        }
        else
        {
            model.RegionName = _facade.FetchRegion(model.RegionId).RegionName;
        }
        TempData["suburbModel"] = model;
        return RedirectToAction("Suburb");
    }

感谢您抽出宝贵的时间

因此,这是我传递模型实例的示例。 我有很多课程的视图,因此我需要单击一个按钮并触发一个动作,这样才能保存所单击课程的所有数据(包括相关ID)。 所以最后,我将所需的实例与隐藏字段一起携带。

我的课程模型...

public class CourseModel
    {
        public int RecordId { get; set; }
        public string StudentNameField { get; set; }
        public string SubjectField { get; set; }
        public string CatalogField { get; set; }
        public string SectionField { get; set; }
        public string InstrNameField { get; set; }
        public string MtgStartField { get; set; }
        public string MtgEndField { get; set; }

    }

我的主要View ...在Views文件夹中称为“ CourseList”

<div id="container">          
<div class="selectLabel">Select a Course:</div><br />
 @foreach (var item in Model)
{           
    @Html.DisplayFor(model=>item)
}
</div>          

我的显示模板-在Shared \\ DisplayTemplates中名为“ CourseModel”的视图...对于您的显示模板,您可以为现有模型和新模型创建唯一模型。 使用displaytemplate中的“现有”模型,它会生成多个表单,每个表单都使用type = submit按钮提交带有模型实例的表单。 使用CSS将按钮建模为链接。 如果仍然需要使用actionlink,请将iD作为参数之一。

@using LecExamRes.Helpers
@model LecExamRes.Models.SelectionModel.CourseModel
@using (Html.BeginForm("CourseList", "Home", null, FormMethod.Post))
{
<div class="mlink">
    @Html.AntiForgeryToken()
    @Html.EncryptedHiddenFor(model => model.RecordId)
    @Html.EncryptedHiddenFor(model => model.CatalogField)
    @Html.EncryptedHiddenFor(model => model.SectionField)
    @Html.EncryptedHiddenFor(model => model.SubjectField)
    @Html.EncryptedHiddenFor(model => model.InstrNameField)
    @Html.EncryptedHiddenFor(model => model.MtgStartField)
    @Html.EncryptedHiddenFor(model => model.MtgEndField)
    <p>
        <input type="submit" name="gbtn" class="groovybutton"      value="@Model.SubjectField - @Model.CatalogField - @Model.SectionField : @Model.InstrNameField">
    </p>  
 </div>
}

我的控制器,课程列表[POST]操作...

  [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult CourseList(SelectionModel.CourseModel model)
    {
        //....do something with my model
       }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM