将值从模型返回到控制器

时间:2014-04-19 01:22:03

标签: c# jquery asp.net-mvc

我有一个应用程序,如果联合所有者==是,它应该显示部分视图。我的Model.cs是

[Display(Name = "Joint Owner")]
public string JointOwner { get; set; }

我的观点是

<div class="form-group">
    @Html.LabelFor(m => m.JointOwner, new { @class = "col-md-3 control-label" })
    <div class="col-md-9">
       <label>@Html.RadioButtonFor(m => m.JointOwner, new { @class = "form-control", value="Yes"})&nbsp;Yes</label>
        <label>@Html.RadioButtonFor(m => m.JointOwner, new { @class = "form-control", value = "No" })&nbsp;No</label>
        @Html.ValidationMessageFor(m => m.JointOwner)
    </div>
</div>

当选择yes值时,我需要它返回一个partialview。我如何在模型中处理这个?或者是否会建议使用javascript / jquery?

public ActionResult PrimaryApplicant(PrimaryApplicantViewModel model)
{
    // Make sure session didn't get eaten.
    var loanType = "";
    if (Session["LoanType"] != null)
    {
        loanType = Session["LoanType"].ToString();
    }

    // Here we decide which view to show next. in the frotn end you may need to handle what to change labels to in the wizard maybe via JQ/JS
    if (loanType == "Auto Refinance")
    {
        return PartialView("AutoRefinance");
    }
    else if (loanType == "Auto Purchase")
    {
        return PartialView("AutoPurchase");
    }
    else
    {
        // You'd want to actually handle it if somehow you got a bad value, I just did it so VS didn't whine.
        return PartialView("PrimaryApplicantPartial");               
    }
}

2 个答案:

答案 0 :(得分:1)

如果你想要一个即时结果而不是在后端/模型上处理它,你会更好。如果您不喜欢局部视图,只需将其设为隐藏的div,如果Joint Owner说是,请将其显示为可见。您可以使用jQuery hide and show

答案 1 :(得分:0)

更优选使用jquery,您可以这样使用:

首先在视图中为部分视图创建容器,即

<div class="table-rec-container"></div>

然后在document.ready()中创建一个函数,如下所示:

function Partialview() {

        var chkradio = $('#JointOwner').val();

        if(chkradio ==true)
        {

        jQuery.ajax({
            url: 'url of partial view',
            type: "GET",
            success: function (data) {

                $('.table-rec-container').html("");
                $('.table-rec-container').html(data);
            }
        });
       }

    }