MVC如何在没有模型的情况下将数据从视图传递到控制器

时间:2016-10-30 18:28:23

标签: asp.net-mvc

我每次复选框时都会提交表单,但是现在我需要传递数据,如果复选框为false或true,但数据永远不会转到我的PriceRange post方法。感谢。

这是我的控制者。我用这个对象将数据传递给我的视图。

public class HomeController : Controller
{
    private FilizTakiEntityDB db = new FilizTakiEntityDB();
    public ActionResult Index()
    {
        Anasayfa obj = new Anasayfa();

        obj.products = db.Products.ToList();
        obj.priceRange = db.PriceRange.ToList();

        return View("Index", obj);
    }

    [HttpPost]
    public ActionResult PriceRange(PriceRange prc , int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var mevcut = db.PriceRange.Find(id);
        mevcut.Fiyat1 = prc.Fiyat1;
        mevcut.Fiyat2 = prc.Fiyat2;
        mevcut.Fiyat3 = prc.Fiyat3;
        return View("Index");
    }

}
public class Anasayfa
{
    public List<PriceRange> priceRange { get; set; }
    public List<Products> products { get; set; }
}

这是我的观点:

  @model  FilizTakiProject.Controllers.Anasayfa

  @using (Html.BeginForm("PriceRange", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
            {
                foreach (var x in Model.priceRange)
                {
                    @Html.CheckBoxFor(ModelItem=>x.Fiyat1)<br />
                    @Html.CheckBoxFor(ModelItem => x.Fiyat2)<br />
                    @Html.CheckBoxFor(ModelItem => x.Fiyat3)
                }
                <input type="submit" name="submit" value="Submit" />
            }

这是我的模特

 public partial class PriceRange
   {
    public int PriceID { get; set; }
    public bool Fiyat1 { get; set; }
    public bool Fiyat2 { get; set; }
    public bool Fiyat3 { get; set; }
   }

0 个答案:

没有答案