locked
how to get releated object using [fromform] RRS feed

  • Question

  • User-1045082127 posted

    hi

    I have one ActionMethod .which it should get dynamic data . I mean when I send (a) , The model binder set (a) or (b) model binder set b . see bellow :

            public class all
            {
                public List<a> project { get; set; }
                public List<b> project { get; set; }
            }
            [HttpPost]
            public IActionResult EditProject([FromForm]all data)
            {
    
           }

    the ModelBinder cant get this syntax and only work with it:

     

    public IActionResult EditProject([FromForm]a data)
            {
    
           }
    

    how can i do it?

    Thursday, October 17, 2019 6:52 AM

Answers

  • User475983607 posted

    The model binder is unable to map the values because the HTML element names do not follow standard naming conventions.   The ViewModel expects a collection of types but the HTML form submits a single item.  Fix the ViewModel to accept single items.  

    public class a
    {
    	public Hazine hazine { get; set; }
    	public DarkhastDahandeHa darkhastDahandeHa { get; set; }
    }

    Next, fix the input names to match the ViewModel.

    <div class="col-md-8">
    	<input type="text" value="6445" name="hazine.HazineDate" class="form-control text-center _IndexMododal" />
    </div>

    IMHO, you have design issues which can cause an over posting vulnerability.  You're allowing clients to populate any of the values in the "a" class.   Generally the goal is limiting the values a client can submit.  Usually this is done in the Action signature using a ViewModel that has only the values a user is allowed to Save/Update.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 18, 2019 4:08 PM

All replies

  • User475983607 posted

    Your code does not compile so clearly you are not showing us the actual run-time code.

    The following pattern will work as long as you follow the standard naming conventions for model binding collections; https://www.learnrazorpages.com/razor-pages/model-binding

    public class All
    {
    	public List<a> ProjectA { get; set; }
    	public List<b> ProjectB { get; set; }
    }
    [HttpPost]
    public IActionResult EditProject([FromForm]All data)
    {
    
    }

    Thursday, October 17, 2019 11:08 AM
  • User-1045082127 posted

    our code does not compile so clearly you are not showing us the actual run-time code.

    it's all I used  and nothing else

    I was read something of above link content and using [bind property] but nothing was happened even by it:

     public class all
            {
                public a project { get; set; }
                public b project { get; set; }
            }
            [HttpPost]
            public IActionResult EditProject([FromForm]all data)
            {
    
           }

    Friday, October 18, 2019 3:43 AM
  • User1634355159 posted

    Hi aminsoraya,

    I think it is because your value passed does not match the background receiving method. Could you tell us what value you want to pass?In addition, I hope that you have noticed the names of two attributes in a class cannot be the same.

    Best Regards,

    Lewis

    Friday, October 18, 2019 6:24 AM
  • User-474980206 posted

    The model binder cannot determine the data type of the json data (because there isn’t one), it can only bind the json data to a specified object type. If you need to bind the json data to a object type determined at post back, you will need to write a custom binder, this custom binder will need to inspect the json data to determine the data type bind do the binding.

    a much better solution would be to have separate methods for each data type.

    Friday, October 18, 2019 9:16 AM
  • User-1045082127 posted

    check <g class="gr_ gr_3 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="3" data-gr-id="3">bellow</g>:

    <form asp-action="EditByFactor"  asp-controller="Home" data-ajax-method="post" data-ajax="true" data-ajax-update="response">
        <div class="row">
            <input type="hidden" value="6445" name="UniqueId" class="form-control text-center _IndexMododal" />
            <div class="col-md-5">
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>تاریخ هزینه :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="HazineDate" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>نوع هزینه :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="HazineType1" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>شناسه پروزه :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="ProjectId" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>مبلغ هزینه :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="Mablagh" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>مبلغ کل پرداختی :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="MablaghPaid" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>عنوان هزینه :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="HazineTitle" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>توضیحات هزینه :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="HazineComment" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-4 text-right">
                        <p>نام فروشگاه :</p>
                    </div>
                    <div class="col-md-8">
                        <input type="text" value="6445" name="ForoshgahName" class="form-control text-center _IndexMododal" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-8">
                        <button class="btn btn-success btn-block end-edit" type="submit" style="font-size:11px;margin-left:10px;">ویرایش تغییرات </button>
                        <button class="btn btn-danger btn-block" data-dismiss="modal" style="font-size:11px;">انصراف </button>
                    </div>
                </div>
            </div>
        </div>
    </form>
    public class a
            {
                public List< Hazine> hazine { get; set; }
                public List<DarkhastDahandeHa> darkhastDahandeHa { get; set; }
            }
    
            [HttpPost]
            public IActionResult EditByFactor([FromForm]a data)
            {
                try
                {
                    // hazine.Update(data);
                    return Ok(new { Message = "با موفقیت ذخیره شد", ErrorNumber = 0 });
                }
                catch
                {
                    return Ok(new { Message = "بروز خاط در سرور ، تغییری رخ نداد ", ErrorNumber = 1 });
                }
    
            }
    public class Hazine
        {
            [Display(Name = "شناسه منحصر بفرد")]
            [Key]
            
            public string UniqueId { get; set; }
    
    
            [Display(Name = "شناسه  هزینه")]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            [BindProperty]
            public int HazineId { get; }
    
    
            [Display(Name = "نوع هزینه")]
            [BindProperty]
            public int? HazineType1 { get; set; } // check paid , check
    
            [Display(Name = "نوع هزینه 2")]
            public int? HazineType2 { get; set; } // check paid , check
    
    
            [Display(Name = "نوع هزینه 3")]
            public int? HazineType3 { get; set; } // check paid , check
                                                  // received
    
    
            [Display(Name = "شناسه  پروژه")]
            [BindProperty]
            public int ProjectId { get; set; }
    
    
            [Display(Name = "مبلغ هزینه ")]
            [BindProperty]
            public long Mablagh { get; set; }
    
    
    
            [Display(Name = "مبلغ کل پرداختی ها ")]
            [BindProperty]
            public long MablaghPaid { get; set; }
    
    
    
            [Display(Name = "تاریخ هزینه ")]
            [StringLength(150)]
            [BindProperty]
            public string HazineDate { get; set; }
    
    
    
            [Display(Name = "تاریخ هزینه ")]
            
            public long HazineDateLong { get; set; }
    
    
            [Display(Name = "عنوان  هزینه ")]
            [StringLength(150)]
            
            public string HazineTitle { get; set; }
    
    
    
            [Display(Name = "توضیحات  هزینه ")]
            [StringLength(500)]
            [BindProperty]
            public string HazineComment { get; set; }
    
    
            [Display(Name = "نام فروشگاه ")]
            [BindProperty]
            [StringLength(150)]
            public string ForoshgahName { get; set; }
    
    
    
            [Display(Name = "تاریخ پرداخت")]
            
            [StringLength(150)]
            public string PayLastDate { get; set; }
    
    
    
            [Display(Name = "تاریخ پرداخت")]
    
            
            public long PayDateLong { get; set; }
        }
    




    Friday, October 18, 2019 3:34 PM
  • User475983607 posted

    The model binder is unable to map the values because the HTML element names do not follow standard naming conventions.   The ViewModel expects a collection of types but the HTML form submits a single item.  Fix the ViewModel to accept single items.  

    public class a
    {
    	public Hazine hazine { get; set; }
    	public DarkhastDahandeHa darkhastDahandeHa { get; set; }
    }

    Next, fix the input names to match the ViewModel.

    <div class="col-md-8">
    	<input type="text" value="6445" name="hazine.HazineDate" class="form-control text-center _IndexMododal" />
    </div>

    IMHO, you have design issues which can cause an over posting vulnerability.  You're allowing clients to populate any of the values in the "a" class.   Generally the goal is limiting the values a client can submit.  Usually this is done in the Action signature using a ViewModel that has only the values a user is allowed to Save/Update.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 18, 2019 4:08 PM