Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
hi
in my index view i have a partial view for send new comments:
HTML
@model NewsViewModel


@Html.Action("create", "Comment", new { NewsId = Model.Id })


my code in comment control is :
C#
// GET: /Comment/Create
       [HttpGet]
       public ActionResult Create(int NewsId)
       {

           ModelState.Clear();
           return PartialView();
       }

       //
       // POST: /Comment/Create

       [HttpPost]
       public ActionResult Create(Comments comments)
       {

           if (ModelState.IsValid)
           {

               db.Comments.Add(comments);
               db.SaveChanges();
               ModelState.Clear();
               return PartialView();

           }
           return PartialView();

       }


my code in Create View:
HTML
@model Nezam.Models.Comments

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset class="fieldset">
        <legend class="legend">ارسال نظر</legend>

        <div class="select">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="input">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="select">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="input">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="select">
            @Html.LabelFor(model => model.Body)
        </div>
        <div class="textarea">
            @Html.EditorFor(model => model.Body)
            @Html.ValidationMessageFor(model => model.Body)
        </div>
        <input type="text" name="NewsId" value="@ViewBag.Id" style="display:none" />
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}


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


Everything is working properly, But When i Refresh Browser (firefox), that ask me :

C#
to display this page , firefox must send information that will repeat action(such as a search or order confirmation) that performed earlier,


next i click "resend" and new record add again .

now what can i do for this problem ?
Posted
Updated 28-Mar-14 0:20am
v2

To avoid such issues you have to maintain the PRG Pattern with ASP.net MVC.

PRG
What is the PRG Pattern ?
PRG stands for "Post/Redirect/Get"
Instead of returning an HTML page directly,
The POST operation returns a redirection command (using the HTTP 303 response code (sometimes 302) together with the HTTP "Location" response header),
Instructing the browser to load a different page using an HTTP GET request
The result page can then safely be bookmarked or reloaded without unexpected side effects


I have written an Article about this.Please read it for more info.

How to Use PRG Pattern with ASP.net MVC
 
Share this answer
 
v3
i changed my code and work very good tank you all

C#
[HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Comments comments)
        {
            comments.DateCreat = DateTime.Now.ToShortDateString();
            if (ModelState.IsValid)
            {
               
                db.Comments.Add(comments);
                db.SaveChanges();
                ModelState.Clear();
                
                ControllerContext.HttpContext.Response.Redirect(Request.UrlReferrer.ToString());            
            }
            
            return PartialView();
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900