0

I used Asp.netMVC and created a view but CreateView page does not get styles and does not send parameters to the database but EditView and DeleteView work.

View Code Iadded CkEditors code under the csHtmls code

@model My_E_Shop.Models.Pages

@{
    ViewBag.Title = "Create" + Server.HtmlDecode(Html.DisplayNameFor(model => model).ToString());
}    

<h2>[email protected](model => model)</h2>

@using (Html.BeginForm("Create", "Pages", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

<div class="form-horizontal">
    @Html.ValidationSummary(true)

    @Html.HiddenFor(model => model.PageID)
    @Html.HiddenFor(model => model.CreateDate)
    @Html.HiddenFor(model => model.PageSee)


    <div class="form-group">
        @Html.LabelFor(model => model.PageTitle, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PageTitle)
            @Html.ValidationMessageFor(model => model.PageTitle)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ShortDescription, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ShortDescription)
            @Html.ValidationMessageFor(model => model.ShortDescription)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.PageText, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.PageText)
            @Html.ValidationMessageFor(model => model.PageText)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ImageName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.Kendo().Upload().Name("imgUp")
            @Html.ValidationMessageFor(model => model.ImageName)
        </div>
    </div>




    <div class="form-group" id="buttons">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-primary" />
            @Html.ActionLink("Back", "Index")
        </div>
    </div>
</div>
}

@section Scripts
{
    <script src="/ckeditor/ckeditor.js"></script>
    <script src="/ckeditor/adapters/jquery.js"></script>

    <script>
        $(function () {
            $('#PageText').ckeditor();
        });
    </script>

}

Controller Code

    [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "PageID,PageTitle,ShortDescription,PageText")]         
Pages pages, HttpPostedFileBase imgUp)
        {
            if (ModelState.IsValid)
            {
                if (imgUp != null)
                {
                    if (CheckContentImage.IsImage(imgUp))
                    {
                        pages.ImageName = Guid.NewGuid().ToString().Replace("-", "") + 
Path.GetExtension
                            (imgUp.FileName);
                    imgUp.SaveAs(Server.MapPath("/PageImages/" + pages.ImageName));

                }
                }
                pages.PageSee = 0;
                pages.CreateDate = DateTime.Now;
                db.Pages.Add(pages);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(pages);
        }
5
  • 1
    What was the problem you can't get PageText value? Commented Oct 16, 2019 at 4:05
  • yes,In CreateView(Mode) page text and Image does not save into database but in EditView and DeleteView Works. Commented Oct 16, 2019 at 23:15
  • Please add one attributes in your action [ValidationInput(false)]. Commented Oct 17, 2019 at 1:49
  • The problem with send parameters(textbox value) Binding in line 3 into the Control code. I deleted Page ID in line 3 and it fixed. Commented Oct 19, 2019 at 22:22
  • You need to include imgUp in your actions parameters similar you have mentioned in Bind(Include = "PageID,PageTitle,ShortDescription,PageText,imgUp")] Pages pages, HttpPostedFileBase imgUp) Commented Oct 20, 2019 at 3:55

2 Answers 2

0

Please check in chrome browsers developer tool all required CSS are loaded or not under source section

Sign up to request clarification or add additional context in comments.

1 Comment

How to check can i check it?
0

You may create a ViewModel class or modify your Pages class by adding HttpPostedFileBase:

public class Pages
{
    public string PageTitle{ get; set; }

    // ... other fields here ...

    public HttpPostedFileBase imgUp { get; set; }
}

And, in the Controller:

[HttpPost]
public ActionResult Create(Pages pages)
{
    // ... 

    if (pages.imgUp != null)
    {
       // ... your code here
    }

    // the rest of code
}

However, if you prefer not adding HttpPostedFileBase to the model class, you may try to handle submit via Ajax. For instance:

var formData = new FormData();    
formData.append( 'file', input.files[0] );

$.ajax({
  url: '/Pages/Create',
  data: formData ,
  dataType: 'json',
  processData: false,
  contentType: false,
  type: 'POST',
  success: function(data){
    alert(data);
  }

For more explanation, you're encouraged to read my answer on this thread: https://stackoverflow.com/a/58354204/4687359

Hope this helped ... :)


Finally, if you were to upload multiple files, modify the model class accordingly:

public List<HttpPostedFileBase> imgUp { get; set; }

more clarification here: https://stackoverflow.com/a/21677152/4687359

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.