嗨,我需要MVC剃刀视图程序来查找输入数字的乘法表(在文本框中输入)

时间:2016-05-30 04:31:35

标签: asp.net-mvc razor

我想获取在文本框中输入的数字的乘法表。

我只想要剃刀视图语法代码。

这是我的观点:

@model StudentDemo.Model.StudentModel
....
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>StudentModel</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Number, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Number, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Number, "", new { @class = "text-danger" })
             </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
             </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

1 个答案:

答案 0 :(得分:0)

您可以尝试这个经过测试的代码。

控制器/行动方法

[HttpGet]
public ActionResult Multiply()
{
    return View();
}

[HttpPost]
public ActionResult Multiply(int num)
{
    string table = "<table>";
    for (int i = 1; i <= 10; i++)
    {
        table += String.Format("<tr><td>{0} * {1} = </td><td>{2}</td></tr>", num, i, num * i);
    }
    table += "</table>";
    ViewBag.NumberTable=table;
    return View();
}

查看

@{
    ViewBag.Title = "Multiply";
}

<h2>Multiply</h2>


@using (Html.BeginForm("Multiply", "Home", FormMethod.Post, ""))
{
    <label>Input Number</label>
    <input type="text" id="num" name="num" />
    <input type="submit" />
} 

@{ 
    string table = ViewBag.NumberTable;
    if (table!="")
    {
        <div> @Html.Raw(table) </div>
    }
}