因此,基本上,我的模型中有一个布尔值,并且在我的视图中,我有一个隐藏字段,当我选择了某个选择框时,我会将其设置为true或false。现在,有了这个,我需要以某种方式检查隐藏字段的值和控制器中的布尔值。如果为true,我想运行一个方法,如果为false,我希望它跳过该方法。

这是我的html,不可见字段位于底部:

<div id="addTransfereeForm" class= "invisible" "whiteborder">
    <form name="addTransferee" id="addTransferee">
    <p><b><u>Add a New Transferee</u></b></p>
        Transfer Effective Date: <input type="text" name="transfereffectivedate"     class="datepicker"<br /> <br />
        Transferee Name: <input type="text" name="transfereename" /><br />
        Transferee Notice Address Line 1: <input type="text" name="transfereenoticeaddressline1" /><br />
        Transferee Notice Address Line 2: <input type="text" name="transfereenoticeaddressline2" /><br />
        Transferee Notice City: <input type="text" name="transfereenoticecity" /><br />
        Transferee Notice State: <input type="text" name="transfernoticestate" /><br />
        Transferee Notice Zip: <input type="text" name="tranfernoticezup" /><br />
        Transferee Notice Phone: <input type="text" name="transfereenoticephone" /><br />
        Transferee Payment Address Line 1: <input type="text" name="transfereepaymentaddressline1" /><br />
        Transferee Payment Address Line 2: <input type="text" name="transfereepaymentaddressline2" /><br />
        Transferee Payment City: <input type="text" name="transfereepaymentcity" /><br />
        Transferee Payment State: <input type="text" name="transfereepaymentstate" /><br />
        Transferee Payment Zip: <input type="text" name="transfereepaymentzip" /><br />
        Transferee Payment Phone: <input type="text" name="transfereepaymentphone" /><br />
        <input type="hidden" name="newtransferee" />
    </form>
</div>


这是我的模型(NewTransferee是我设置为true或false的布尔值):

 public class CreateTransferee
{
    public DateTime? TransferEffectiveDate { get; set; }
    public string TransfereeName { get; set; }
    public string TransfereeNoticeAddressLine1 { get; set; }
    public string TransfereeNoticeAddressLine2 { get; set; }
    public string TransfereeNoticeCity { get; set; }
    public string TransfereeNoticeState { get; set; }
    public string TransfereeNoticeZip { get; set; }
    public string TransfereeNoticePhone { get; set; }
    public string TransfereePaymentAddressLine1 { get; set; }
    public string TransfereePaymentAddressLine2 { get; set; }
    public string TransfereePaymentCity { get; set; }
    public string TransfereePaymentState { get; set; }
    public string TransfereePaymentZip { get; set; }
    public string TransfereePaymentPhone { get; set; }
    public bool NewTransferee { get; set; }
}


这是我的控制器(我需要对其进行编辑以使其在运行结束部分之前检查bool值):

 [NoCache]
    [HttpPost]
    [ValidateAntiForgeryToken]

    public JsonResult ResolveProfileSelectionRequired(ResolveProfileSelectionRequiredModel model)
    {
        var resolved = SqlDataProvider.ResolveProfileSelectionRequiredJobException(model);

        return resolved ? Json("Success") : Json("Failed");
    }


最后,如果bool为true(位于SQLDataProvider.cs中),我将创建运行的方法:

public static int CreateNewTransferee(CreateTransferee model)
    {

        using (var db = new NJB.NJBDataContext())
        {
            var result = db.SP_AddTransferee(
            model.TransferEffectiveDate,
            model.TransfereeName,
            model.TransfereeNoticeAddressLine1,
            model.TransfereeNoticeAddressLine2,
            model.TransfereeNoticeCity,
            model.TransfereeNoticeState,
            model.TransfereeNoticeZip,
            model.TransfereeNoticePhone,
            model.TransfereePaymentAddressLine1,
            model.TransfereePaymentAddressLine2,
            model.TransfereePaymentCity,
            model.TransfereePaymentState,
            model.TransfereePaymentZip,
            model.TransfereePaymentPhone);

           return (int) result.ReturnValue;
        }
    }

最佳答案

如果您需要做的只是检查布尔值,那只是一个条件语句:

public JsonResult ResolveProfileSelectionRequired(ResolveProfileSelectionRequiredModel model)
{
    if (model.NewTransferee)
    {
        // do something
    }
    else
    {
        // do something else
    }
}


目前尚不清楚目标是什么,因为当前的return语句取决于您只想有条件调用的方法的输出。所有代码路径都需要返回某些内容。也许是这样吗?:

public JsonResult ResolveProfileSelectionRequired(ResolveProfileSelectionRequiredModel model)
{
    if (model.NewTransferee)
    {
        var resolved = SqlDataProvider.ResolveProfileSelectionRequiredJobException(model);

        return resolved ? Json("Success") : Json("Failed");
    }
    else
    {
        return Json("Success");
    }
}


同样,如果您不了解更多有关此操作的预期行为,我将无法知道。

值得注意的是,这实际上不是“ MVC”。最简单的说,此操作方法只是一种接收类型为ResolveProfileSelectionRequiredModel的参数的方法。条件语句只是纯C#。所有返回值的代码路径均为纯C#。关于此方法的唯一“ MVC”是它返回一个JsonResult



编辑:我刚刚注意到...您正在寻找的值是在CreateTransferee模型上,但是您的控制器正在接收ResolveProfileSelectionRequiredModel?该值在ResolveProfileSelectionRequiredModel上吗?它们似乎非常相似。如果不在该模型上,则可以将其添加到该模型中,也可以使用@Raidri的方法将其作为显式参数添加到控制器操作中。

关于c# - 检查隐藏的bool值运行方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18983884/

10-09 19:22
查看更多