Walidacja modelu w PartialView

0

Witam!
Mam problem z walidacją modelu w PartialView.
Model:

 public partial class transaction
    {
        public int transaction_id { get; set; }
        [Required]
        public string title { get; set; }
        public Nullable<int> account_from_id { get; set; }
        public Nullable<int> account_to_id { get; set; }
        [Required]
        [RegularExpression(@"^[0-9]+(\.[0-9]{1,2})$", ErrorMessage = "Zła wartość")]
        public Nullable<decimal> value { get; set; }
        public Nullable<System.DateTime> datetime { get; set; }
        public string description { get; set; }

        public virtual account account { get; set; }
    }

PartialView:

@model Banke.Models.transaction

@{
    ViewBag.Title = "Create";

}
<script src="~/Scripts/jquery-3.3.1.min.js" language="javascript" type="text/javascript"></script>
<script src="~/Scripts/jquery-3.3.1.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<h2>Wyślij przelew</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            <p class="control-label col-md-2">
                Tytuł:
            </p>
            <div class="col-md-10">
                @Html.EditorFor(model => model.title, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.title, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.account_from_id, "Konto:", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("account_from_id", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.account_from_id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <p class="control-label col-md-2">Odbiorca </p>
            <div class="col-md-10">
                @Html.EditorFor(model => model.account_to_id, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.account_to_id, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <p class="control-label col-md-2">
                Kwota:
            </p>
            <div class="col-md-10">
                @Html.EditorFor(model => model.value, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.value, "", new { @class = "text-danger" })
            </div>
        </div>



        <div class="form-group">
            <p class="control-label col-md-2">
                Opis:
            </p>
            <div class="col-md-10">
                @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Wyślij" class="btn btn-default" />
            </div>
        </div>
    </div>
}



@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Wywołanie Partial View:

       @Ajax.ActionLink("Przelew", "CreateTransaction", "Transaction", new AjaxOptions
       {
           HttpMethod = "GET",
           UpdateTargetId = "divTrans",
           InsertionMode = InsertionMode.Replace,
           OnBegin = "RunThisThing",
           LoadingElementId = "image_loading"


            })

Kontroler:

  public PartialViewResult CreateTransaction()
        {
            ViewBag.account_from_id = new SelectList(db.accounts.Where(x => x.user.login == User.Identity.Name && x.account_type_id == 1), "account_id", "name");

            return PartialView("_Create");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize]
        public ActionResult CreateTransaction([Bind(Include = "transaction_id,title,account_from_id,account_to_id,value,datetime,description")] transaction transaction)
        {

            if (ModelState.IsValid)
            {

                dao.TransferFunds(transaction);
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.account_from_id = new SelectList(db.accounts, "account_id", "name", transaction.account_from_id);
                return PartialView("_Create", transaction);
            }

        }

Czy ma ktoś z was jakiś pomysł?

0

Jeśli wczytujesz ajaxem do DOM partial View to walidacja Ci nie zadziała raczej. Są na to sposoby ale trzeba pogooglac.

1 użytkowników online, w tym zalogowanych: 0, gości: 1