【问题标题】:View doesn't pass Model to Action using Ajax.ActionLink视图不使用 Ajax.ActionLink 将模型传递给操作
【发布时间】:2013-12-11 12:10:02
【问题描述】:

我的模特

  public class CalculateModel
    {
        public decimal ItemPrice { get; set; }
        public int CategoryID { get; set; }
        public decimal CategoryTax { get; set; }
        public decimal CategoryDuty { get; set; }
        public decimal value { get; set; }
        public decimal fees { get; set; }
        public int weightValue { get; set; }
        public IEnumerable<SelectListItem> CategoriesList { get; set; }
        public IEnumerable<SelectListItem> WeightsList { get; set; }
    }

我的控制器:

 public ActionResult Index()
        {

            ViewBag.CategoriesNames = _db.Categories.ToList();
            ViewBag.Weights = _db.Weights.ToList();

            return View();
        }


        public ActionResult _Calculate(CalculateModel model)
        {
             decimal Total;
            model.CategoryTax = _db.Categories.Where(r => r.CategoryID == model.CategoryID).FirstOrDefault().Tax;
            model.CategoryDuty = _db.Categories.Where(r => r.CategoryID == model.CategoryID).FirstOrDefault().Duty;
            var weightfees = model.weightValue*5.5;
            Total = model.ItemPrice +(model.CategoryDuty*model.ItemPrice/100) +(model.CategoryTax*model.ItemPrice/100)+(decimal)weightfees;
            if (Total <100)
            {
                Total = Total+5;
            }
            else
            {
                Total = Total + (Total*5/100);
            }
            var CurrencyChange = _db.Currencies.Where(x=>x.Name == "Dollars").FirstOrDefault().Value;

            string Message = "Total = " + Total + "$ Which equals"+ Total*CurrencyChange+"LE";
            return Content(Message);
        }

我的看法:

  @model CSP1225.Models.CalculateModel
      @Html.TextBoxFor(m => m.ItemPrice, new { id="price"})
                                        <label class="control-label">Item Type</label>
                                           @{
                                               var Categories = ViewBag.CategoriesNames;}
@Html.DropDownListFor(m => m.CategoryID, new SelectList(Categories, "CategoryID", "CategoryName"), new { id= "SelectedCategory" })
                                                @{var weights = ViewBag.Weights;}
                                                @Html.DropDownListFor(m=> m.weightValue, new SelectList(weights,"WeightID","Name"), new {id="weight"})


    @Ajax.ActionLink("Calculate","_Calculate","Home",Model,new AjaxOptions { UpdateTargetId = "result" },null)                
                                </div>

我的问题是当Ajax.ActionLink被点击时,传递给Controller函数的模型变成了null,我之前遇到过这个问题,直到现在还没有找到灵魂,任何人都可以帮助我吗???

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 asp.net-ajax


    【解决方案1】:

    您应该改用Ajax.BeginForm() 来正确发布模块。

    查看

    @model CSP1225.Models.CalculateModel
    
    @using(Ajax.BeginForm("_Calculate","Home",new AjaxOptions { UpdateTargetId = "result" }) {
          @Html.TextBoxFor(m => m.ItemPrice, new { id="price"})
                                            <label class="control-label">Item Type</label>
                                               @{
                                                   var Categories = ViewBag.CategoriesNames;}
    @Html.DropDownListFor(m => m.CategoryID, new SelectList(Categories, "CategoryID", "CategoryName"), new { id= "SelectedCategory" })
                                                    @{var weights = ViewBag.Weights;}
                                                    @Html.DropDownListFor(m=> m.weightValue, new SelectList(weights,"WeightID","Name"), new {id="weight"})
    
    
        <button type="submit">Submit</button>
    }      
    

    然后,将HttpPost 属性添加到您的_Calculate ActionMethod。

    【讨论】:

    • 现在我想在&lt;div id="result"&gt;&lt;/div&gt; 中查看结果,但另一个页面加载了我想查看的消息.. 请帮助我
    • 尝试将_CalculateMessage 的返回类型更改为ContentResultPartialViewResult
    • 我把它改成了ContentResult,但同样的问题,我试过PartialViewResult,但它不接受字符串
    猜你喜欢
    • 2015-02-11
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2019-02-01
    相关资源
    最近更新 更多