【问题标题】:Set condition for Controller为控制器设置条件
【发布时间】:2019-08-25 16:43:38
【问题描述】:

我有一个这样的 html 表单:

<div class="form-group">                                                                                           
    <labelfor="exampleSelectd">Default Select</label>
    <select class="form-control" id="Selection">
        <option value="Teacher">Teacher</option>
        <option value="Parent">Parent</option>
    </select>
</div>

和一个从上面的表格中获取价值的脚本:

$.post("Home/Print", {          
    Selection: $("#Selection").val(),
....
})

如何在控制器上设置条件? I mean when the user chooses &lt;option value="Teacher"&gt;Teacher&lt;/option&gt;, my Controler will return WriteFile( ...A.docx....); and when chooses &lt;option value="Parent"&gt;Parent&lt;/option&gt;, the controller will return WriteFile( ...B.docx....);

我的控制器是这样的:

[HttpPost]
public string Print(FormModel document)
{
    if ( condition ) {
        return WriteFile(...A.docx....);
    }
else {
        return WriteFile( ...B.docx....);
    }                    
}    

public class FormModel 
{    
    public string Selection { get; set; }
    ...
}

【问题讨论】:

    标签: c# html asp.net-mvc model-view-controller


    【解决方案1】:

    FormModel 类被填充并注入到public string Print 方法中。可以使用 getter 检索模型/dto 的属性:

    class FormController
    {
        [HttpPost]
        public string Print(FormModel document)
        {
            if (document.Selection.Equals("Teacher"))
            {
                return WriteFile(...A.docx....);
            }
            else
            {
                return WriteFile(...B.docx....);
            }
        }
    
    
    }    
    public class FormModel {
        public string Selection { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-12-04
      • 2012-03-27
      • 2017-02-12
      • 2015-11-06
      • 2012-05-18
      • 1970-01-01
      • 2015-02-09
      • 2011-11-03
      • 1970-01-01
      相关资源
      最近更新 更多