简体   繁体   English

从PartialView MVC3的父级访问值

[英]Access value from parent from PartialView MVC3

i need to maintain student interest in edit mode there is partial view where two list boxes - one for current interest and other for available (which are not added in student profile) each interest has a ajax Action link. 我需要在编辑模式下保持学生的兴趣,这里有部分视图,其中有两个列表框-一个用于当前兴趣,另一个用于可用(未在学生资料中添加),每个兴趣都有ajax Action链接。 when i click on it it add to student current interest. 当我单击它会增加学生当前的兴趣。 problem is that-- i need to send student id with it which is from parent view( ie not from partial view), and student id is in url or -- how can i access student id in partial view 问题是-我需要从父视图发送学生证(即不是从部分视图),而学生证在URL中,或者-我如何在部分视图中访问学生证

// Code sample //代码示例

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

I have to pass model.StId with Ajax Actionlink like @Ajax.ActionLink("Add", "AddInterest", new { id = item.IsId, StId = @Model.StId }, 我必须通过带有Ajax Actionlink的@ Ajax.ActionLink(“ Add”,“ AddInterest”,new {id = item.IsId,StId = @ Model.StId},传递model.StId

In controller 在控制器中

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

You could pass the student id to the PartialAddTool child action: 您可以将学生ID传递给PartialAddTool子操作:

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

and now the controller action will have the student id: 现在控制器操作将具有学生ID:

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

Now all that's left is have the PartialAddTool action set some property on the view model that is passed to the partial view. 现在剩下的就是让PartialAddTool操作在传递给局部视图的视图模型上设置一些属性。 Currently this partial view seems to be strongly typed to IEnumerable<StdMan.Models.Interest> but you could create a new view model which has 2 properties: the student id and a collection of interests. 当前,此局部视图似乎已强类型IEnumerable<StdMan.Models.Interest>但您可以创建一个具有2个属性的新视图模型:学生ID和兴趣集合。 This way the partial view will have the student id and it could be used when generating the links to finally be passed to the AddInterest action. 这样,部分视图将具有学生ID,并且可以在生成最终传递给AddInterest操作的链接时使用它。

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

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