表单绑定:将模型更改为FormModel

时间:2018-09-20 11:14:38

标签: jquery asp.net-core-mvc model-binding asp.net-core-tag-helpers

在ASP.NET Core MVC中,我将模型绑定用于级联下拉列表,但是现在我试图将其他字段集成到我的表单中,因为我习惯于使用FormModel,所以我陷入了困境。

我需要的是两个绑定到数据库的级联下拉列表,两个单选按钮和另一个字段以及一个“评估”按钮,它将用户重定向到另一个视图(称为“评估”),在这里我可以使用模型来检索值称为BasicEvaluationForm,如下图所示

[HttpPost]
public IActionResult Evaluate(BasicEvaluationForm form)

下面您会从模型,控制器和视图中找到一些代码摘录

感谢您的帮助!

用于级联下拉菜单的模型

public class Make
{
    [Key]
    public int MakeID { get; set; }
    public string MakeName{ get; set; }
    [NotMapped]
    public int ModelID { get; set; }
}

public class Model
{
    [Key]
    public int ModelID { get; set; }
    public int MakeID { get; set; }
    public string ModelName { get; set; }
}

BasicEvaluationForm我正在尝试集成

public class BasicEvaluationForm
{
    public Model model { get; set; }
    public Make make { get; set; }
    public string gearingType { get; set; }
    public int mileage { get; set; }
}

控制器

public class HomeController : Controller
{
    private readonly DatabaseContext _context;

    public HomeController(DatabaseContext context)
    {
        _context = context;
    }

    [HttpPost]
    public IActionResult Evaluate(BasicEvaluationForm form)
    {
        if (ModelState.IsValid)
        {
            try
            {
                string makeTxt = form.model;
                string modelTxt = form.make;
                string kilometrage = form.kilometrage.ToString();
                string gearingType = form.gearingType;

                var model = new PredictionService().PredictPrice(makeTxt, modelTxt, mileage, gearingType).Result;
                return View("Evaluate", model);
            }
            catch (Exception e)
            {
                ViewBag.Error = e.Message;
            }
        }
        return View();
    }

    [HttpPost]
    public IActionResult Index(Make objcategory, FormCollection formCollection)
    {
        var ModelID= HttpContext.Request.Form["ModelID"].ToString();

        List<Make> makelist = new List<Make>();
        makelist = (from make in _context.Make
                    select make).ToList();
        makelist.Insert(0, new Make { MakeID = 0, MakeName = "Select" });

        ViewBag.ListofMake = makelist;
        return View(objcategory);
    }

    public JsonResult GetModel(int MakeID)
    {
        List<Model> modellist = new List<Model>();

        modellist = (from model in _context.Model
                     where model.MakeID == MakeID
                     select model).ToList();

        modellist.Insert(0, new Model { ModelID = 0, ModelName = "Select" });


        return Json(new SelectList(modellist, "ModelID", "ModelName"));
    }

    public IActionResult Index()
    {
        List<Make> makeList = new List<Make>();
        makeList = (from make in _context.Make select make).ToList();
        makeList.Insert(0, new Make { MakeID = 0, MakeName = "Select" });

        ViewBag.ListOfMake = makeList;
        return View();
    }
  }
}

查看

@model whatstheprice.io.basic.Models.Make

@{
<script src="/lib/jquery/dist/jquery.js"></script>

<script type="text/javascript">
    $(document).ready(function () {
        var items = "<option value='0'>Select</option>";
        $('#ModelID').html(items);
    });
</script>

<script type="text/javascript">
    $(document).ready(function () {
        $('#MakeID').change(function () {
            var url = '@Url.Content("~/")' + "Home/GetModel";
            var ddlsource = "#MakeID";
            $.getJSON(url, { MakeID: $(ddlsource).val() }, function (data) {
                var items = '';
                $("#ModelID").empty();
                $.each(data, function (i, model) {
                    items += "<option value='" + model.value + "'>" + model.text + "</option>";
                });
                $('#ModelID').html(items);
            });
        });
    });
</script>

<form asp-controller="Home" asp-action="Index" method="post" class="form-horizontal" role="form">
    <div class="form-group">
        <div class="row">
            <div class="alert-danger" asp-validation-summary="ModelOnly"></div>
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                <label asp-for="MakeName" class="control-label"></label>
                <select asp-for="MakeID"
                        class="form-control"
                        asp-items="@(new SelectList(@ViewBag.ListofMake,"MakeID", "MakeName"))"></select>
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
                <label class="control-label">ModelName</label>
                <select class="form-control" id="ModelID" name="ModelID" asp-for="ModelID"
                        asp-items="@(new SelectList(string.Empty,"ModelID", "ModelName"))"></select>
            </div>
        </div>
    </div>
</form>
}

0 个答案:

没有答案