fxmle
fxmle

Reputation: 129

Submitting an actionLink to a form mvc4

We have a list of action links

Partial View

@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>

I assume that this isn't the correct way to do this, but if you could point me in the right direction it would be appreciated. I want to submit this data into an existing form

Region View

@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>
}

So you can either submit a new one or submit an existing one. Im not sure how to get the id of an existing one into my model. Here is the controller:

    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");
    }

Thanks for taking the time

Upvotes: 1

Views: 201

Answers (1)

foxtrotZulu
foxtrotZulu

Reputation: 1119

So heres my example of passing an instance of a model. I've got a view with many courses so I need to click a button and fire an action, thus carrying all data (including relevant ID) of the course clicked. So in the end I carry the instance I need with the hidden fields.:)

My course model...

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; }

    }

My main View...Called "CourseList" in Views folder

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

My Display template - Its a view called "CourseModel" in Shared\DisplayTemplates ...For your display template, you could make a unique model for existing & new. Using your "existing" model in the displaytemplate, it results in multiple forms, each using a button type=submit to submit the form with model instance. Use CSS to model the button like a link. If you still need to use actionlink, carry the iD as one of the params.

@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>
}

My controller, Courselist [POST] Action...

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

Upvotes: 2

Related Questions