如何通过Ajax.BeginForm将DropDownList Selected Value传递给Controller?

时间:2016-12-23 21:10:20

标签: javascript c# jquery html model-view-controller

我无法将数据传递给控制器​​

控制器

 [Route("{restaurantId}/Detail/AddDistrict")]
            public virtual ActionResult AddDistrict(long? restaurantId)
            {
                if (restaurantId == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                var restaurant = _restaurantService.GetById(Convert.ToInt64(restaurantId));
                if (restaurant == null) return HttpNotFound();
                var resDistrict = _restaurantService.GetLocationByRestaurantId(Convert.ToInt64(restaurantId));
                var districts = _locationService.
                    GetDistrictsByCityName("اهواز");
                ViewBag.Districts = new SelectList(districts, "Id", "Name");
                return PartialView(MVC.Admin.Restaurants.Views.Location._AddDistrict, new AddRestaurantLocationViewModel { RestaurantId = restaurant.Id });


 [AjaxOnly]
        [Route("{restaurantId}/Detail/AddDistrict")]
        [HttpPost]
        public virtual ActionResult AddDistrict(long? restaurantId, long districtId)
        {
            var model = new AddRestaurantLocationViewModel
            {
                DistrictId = districtId,
                RestaurantId = Convert.ToInt64(restaurantId),
            };
            _restaurantService.SetLocation(model);
            return RedirectToAction(MVC.Admin.Restaurants.ActionNames.Location, new { restaurantId = restaurantId });
        }

_AddDistrict.cshtml 作为VIEW

@model ViewModels.Admin.Restaurant.Location.AddRestaurantLocationViewModel

@using (Ajax.BeginForm(MVC.Admin.Restaurants.ActionNames.AddDistrict, MVC.Admin.Restaurants.Name, null, new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "locations", InsertionMode = InsertionMode.Replace }, htmlAttributes: new { @class = "inline-form", role = "form" }))
{

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.DistrictId, htmlAttributes: new { @class = "control-label" })
        <text> : </text>
        @Html.DropDownListFor(model => model.DistrictId, ViewBag.Districts as IEnumerable<SelectListItem>, new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.DistrictId, "", new { @class = "text-danger" })
    </div>

    @Html.SubmitGlyph("ثبت", "fa fa-plus", new { @class = "btn btn-success" })
}

AddRestaurantLocationViewModel 作为模型

  public class AddRestaurantLocationViewModel
    {
        [Required(ErrorMessage = "وارد کردن {0} الزامیست")]
        [DisplayName("منطقه")]
        public long DistrictId { get; set; }


        public long RestaurantId { get; set; }
    }

当我提交时,返回此错误:

    jquery-2.2.4.js:9175 POST http://localhost:30936/Admin/Restaurants/4/Detail/AddDistrict 500 (Internal Server Error)
    send @ jquery-2.2.4.js:9175
    ajax @ jquery-2.2.4.js:8656
    asyncRequest @ jquery.unobtrusive-ajax.js:128
    (anonymous) @ jquery.unobtrusive-ajax.js:183
    dispatch @ jquery-2.2.4.js:4737
    elemData.handle @ jquery-2.2.4.js:4549
    VM3829:3 XHR Loaded (AddDistrict - 500 Internal Server Error - 23.446000006515533ms - 12.553KB)
http://localhost:30936/Admin/Restaurants/4/Detail/AddDistrict?districtId=3&X-Requested-With=XMLHttpRequest
VM3831:3 XHR Data Object {startedDateTime: "2016-12-23T20:52:49.298Z", time: 23.446000006515533, request: Object, response: Object, cache: Object…}
VM3832:3 Response <!DOCTYPE html>
<html>
    <head>
        <title>Value cannot be null.<br>Parameter name: entity</title>
        <meta name="viewport" c...

最后我无法将selectedValue传递给控制器​​

0 个答案:

没有答案