retrieve model properties values inside GET method in mvc

不羁岁月 提交于 2019-12-12 03:58:28

问题


I have following GET method , that's a code to a create form

 public ActionResult Add_Product(string Product_ID)
 { 
        AddNewProduct sample = new AddNewProduct();

        return View(sample);
 }

this is the model class for that

public class AddNewProduct
{
    public string Product_ID { get; set; }

    ...
}

this is that create form

    @model project_name.Models.AddNewProduct    

    <h4>Add New Product</h4>    

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

        <div class="form-horizontal">                
            @Html.ValidationSummary(true, "", new { @class = "text-danger" }) <div class="form-group">
                @Html.LabelFor(model => model.Product_ID, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Product_ID, new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Product_ID, "", new { @class = "text-danger" })
               </div>
         </div>

         .....

<div>
    @Html.ActionLink("Back to AddNewProduct", "AddNewProduct","Home" , new {Product_ID = Model.Product_ID})
</div>

    }

I can Insert a Product_ID using this view page .But Once I click this Back to AddNewProduct link and debug AddNewProduct I cannot see any value for string Product_ID

Why this model properties not bind well


回答1:


You need to assign value. Assign value of Product_ID which you are sending from get method to Product_ID property of class

public ActionResult Add_Product(string Product_ID)
     { 
            AddNewProduct sample = new AddNewProduct();
            sample.Product_ID = Product_ID;
            return View(sample);
     }



回答2:


To pass the value of the textbox to the Add_Product() GET method, you need to use javascript/jquery. Replace you @Html.ActionLink(..) with

<a href="#" class="back">Back to AddNewProduct</a>

and add the following script

var baseUrl = '@Url.Action("Add_Product", "Home")';
$('#back').click(function() {
    var id = $('#Product_ID').val();
    location.href = baseUrl + '/' + id;
}}

Note: location.href = baseUrl + '/' + id; assumes your have defined a specific route with {controller}/{action}/{Product_ID}, otherwise it needs to be

location.href = baseUrl + '?Product_ID=' + id;

Alternatively, change the method parameter to string id so it uses the default route

Note also that you will probably want to change the method to

public ActionResult Add_Product(string Product_ID)
{ 
    AddNewProduct sample = new AddNewProduct
    {
        Product_ID = Product_ID
    };
    return View(sample);
}

so that if you click the Back to AddNewProduct link, the view will display the previous value you entered.




回答3:


The second parameter of the @Html.ActionLink is the actionName but you sent the model name (AddNewProduct). Change it to this:

@Html.ActionLink("Back to AddNewProduct", "Add_Product","Home" , new {Product_ID = Model.Product_ID})

Or use this overload (You need to send null also when using this ActionLink overload):

@Html.ActionLink("Back to AddNewProduct", "Add_Product","Home" , new {Product_ID = Model.Product_ID}, null)


来源:https://stackoverflow.com/questions/34649082/retrieve-model-properties-values-inside-get-method-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!