0

編集モードで学生の関心を維持する必要があります。2つのリストボックスがある部分的なビューがあります。1つは現在の関心用で、もう1つは利用可能(学生のプロファイルには追加されません)用です。各関心にはajaxアクションリンクがあります。私がそれをクリックすると、それは学生の現在の興味に追加されます。問題は、-親ビューから(つまり、部分ビューからではなく)の学生IDを送信する必要があり、学生IDがURLにあるか-部分ビューで学生IDにアクセスするにはどうすればよいですか?

//コードサンプル

@model StdMan.Models.Student

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="../../../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>



@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Project</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.StId)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.StId)
            @Html.ValidationMessageFor(model => model.StId)
        </div>

    <div class="editor-label">
            Intested In              
        </div>
        <div class="editor-field" id="Interest">
          @Html.Action("PartialAddInterest", "Interest")
        </div>  

     </fieldset>
    <p>
            <input type="submit" value="Save" />
        </p>
}

////////// --PartialView PartialAddInterest.cshtml

@model IEnumerable<StdMan.Models.Interest>

<table>
@foreach (var item in ViewBag.Added)
{
    <tr>
        <td>@item.Name

            @Ajax.ActionLink("Remove", "RemoveInterest",new { id = item.IsId },
    new AjaxOptions
    {
        UpdateTargetId = "Interest",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "GET"
    })
        </td>
    </tr>
}
</table>

<table>

@foreach (var item in ViewBag.Rem)
{
    <tr>
        <td>@item.Name

            @Ajax.ActionLink("Add", "AddInterest", new { id = item.IsId },
    new AjaxOptions
    {
        UpdateTargetId = "Interest",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "GET"
    })
        </td>
    </tr>
}

</table>

@ Ajax.ActionLink( "Add"、 "AddInterest"、new {id = item.IsId、StId = @ Model.StId}、のようなAjaxActionlinkでmodel.StIdを渡す必要があります

コントローラー内

 public ActionResult AddInterest( int id, int StId)
 {
          //logic to add interest in specific student profile depend on StId
 }
4

1 に答える 1

0

学生IDをPartialAddTool子アクションに渡すことができます。

@Html.Action("PartialAddTool", "Interest", new { id = Model.StId })

これで、コントローラーアクションの学生IDは次のようになります。

public ActionResult PartialAddTool(int stdId)
{
    ...
}

あとはPartialAddTool、部分ビューに渡されるビューモデルにプロパティを設定するアクションを設定するだけです。現在、この部分ビューは強く型付けされているようIEnumerable<StdMan.Models.Interest>ですが、学生IDと興味のあるコレクションの2つのプロパティを持つ新しいビューモデルを作成できます。AddInterestこのように、部分ビューには学生IDが含まれ、最終的にアクションに渡されるリンクを生成するときに使用できます。

于 2012-05-31T09:50:54.477 に答える